Welcome to MSDN Blogs Sign in | Join | Help

The Best Culture Invariant Format for DateTime

If you are looking to display how to display DateTime as text without causing confusion to users in different countries then good choices is either "o" or "r". The "o" format is in general more preferable as it also puts timezone offset.

 

long t = DateTime.Now.Ticks;
 
Console.WriteLine((new DateTime(t)).ToString("o"));
 
Console.WriteLine((new DateTime(t, DateTimeKind.Local)).ToString("o"));
 
Console.WriteLine((new DateTime(t, DateTimeKind.Unspecified)).ToString("o"));
 
Console.WriteLine((new DateTime(t, DateTimeKind.Utc)).ToString("o"));

 

Prints followings when actual date time is 2009-11-08T17:16:13.7791953 PST:
2009-11-08T17:16:13.7791953
2009-11-08T17:16:13.7791953-08:00
2009-11-08T17:16:13.7791953
2009-11-08T17:16:13.7791953Z


If you use "r" instead it would print followings:
Sun, 08 Nov 2009 17:26:02 GMT
Sun, 08 Nov 2009 17:26:02 GMT
Sun, 08 Nov 2009 17:26:02 GMT
Sun, 08 Nov 2009 17:26:02 GMT

Published Sunday, November 08, 2009 5:35 PM by shitals

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker