Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Formatting current time

Tags:

In C#, how can I get the current DateTime in the following format? 2011-08-10T21:36:01.6327538Z

like image 399
PercivalMcGullicuddy Avatar asked Oct 12 '11 23:10

PercivalMcGullicuddy


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


2 Answers

DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ")

Keep in mind that DateTime.Now is sometimes only precise to a thousandth of a second, depending on the system clock. This page shows the following:

It is possible to display very small fractional units of a second, such as ten thousandths of a second or hundred-thousandths of a second. However, these values may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On Windows NT 3.5 and later, and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.

However, if you populate the DateTime yourself, you can make it more precise. I am not aware of any other built-in libraries that are more precise than DateTime.UtcNow.

Also, DateTime.UtcNow.ToString("o") will give you an ordinal datetime string. This doesn't specify the timezone at the end, so you'd still need to add Z to the end if you were dealing with Utc

like image 60
Christopher Currens Avatar answered Sep 22 '22 22:09

Christopher Currens


If you want your times in UTC (which is what the Z implies) then you need to ensure that they are UTC times...

i.e.

DateTime.UtcNow.ToString("O");

or assuming that you know that your datetime is local...

DateTime foo = MethodThatReturnsALocalTime();
foo.ToUniversalTime().ToString("O");

FWIW: DateTime.UtcNow is faster than DateTime.Now because it doesn't need to do a timezone lookup, on Compact Framework that difference can be very noticeable for some reason.

like image 45
Paul Roberts Avatar answered Sep 23 '22 22:09

Paul Roberts