30

I have to pre-validate in Javascript a string that will be a DateTime in c#. The DateTime parse uses InvariantCulture.

Does anyone know the DateTime formats defined for InvariantCulture?

nacho1493
  • 447
  • 1
  • 5
  • 11

4 Answers4

40

Compiling information from standard date and time format strings:

Pattern Example
Short Date Pattern ("d") MM/dd/yyyy
Long Date Pattern ("D") dddd, dd MMMM yyyy
Full Date Short Time ("f") dddd, dd MMMM yyyy HH:mm
Full Date Long Time ("F") dddd, dd MMMM yyyy HH:mm:ss
General Date Short Time ("g") MM/dd/yyyy HH:mm
General Date Long Time ("G") MM/dd/yyyy HH:mm:ss
Month ("M", "m") MMMM dd
Round-Trip ("O", "o") yyyy-MM-ddTHH:mm:ss.fffffffK
RFC1123 ("R", "r") ddd, dd MMM yyyy HH:mm:ss GMT
Sortable ("s") yyyy-MM-ddTHH:mm:ss
Short Time ("t") HH:mm
Long Time ("T") HH:mm:ss
Universal Sortable ("u") yyyy-MM-dd HH:mm:ssZ
Universal Full ("U") dddd, dd MMMM yyyy HH:mm:ss
Year Month ("Y", "y") yyyy MMMM
KyleMit
  • 30,350
  • 66
  • 462
  • 664
Tony Morris
  • 947
  • 8
  • 13
  • 35
    Seeming as `MM/dd/yyyy` is used almost exclusively by Americans (`en-US`) but `InvariantCulture` is ostensibly meant to be universal, it would have been better if `InvariantCulture` used ISO 8601 (`yyyy-MM-dd` and `HH:mm:ss`) date and time formats instead of its current format. It means I can't use `InvariantCulture` for appropriate culture-neutral logging, for example. – Dai Oct 05 '19 at 09:57
  • To expand on my comment above (from 3 years ago) [I have posted copy-and-pastable code that gives you `InvariantCultureWithIso8601` which is an immutable ISO 8601 `CultureInfo` instance, with some handy `DateTime` extension methods too](https://stackoverflow.com/a/12447289/159145). HTH. – Dai Dec 09 '22 at 19:22
  • @Dai if you know what you want (yyyy-MM-dd and HH:mm:ss), then you can specify that. InvariantCulture just make some guarantees, but it doesn't matter that much which ones are picked, as long as they never change. – Luc Bloom Dec 21 '22 at 11:53
14

It's very easy to test.

public static void Main()
{
    var d = DateTime.Now;

    Console.WriteLine("Date format (long):  {0}", d.ToString("D", CultureInfo.InvariantCulture));
    Console.WriteLine("Date format (short): {0}", d.ToString("d", CultureInfo.InvariantCulture));
    Console.WriteLine("Full format (long):  {0}", d.ToString("F", CultureInfo.InvariantCulture));
    Console.WriteLine("Full format (short): {0}", d.ToString("f", CultureInfo.InvariantCulture));
    Console.WriteLine("Time format (long):  {0}", d.ToString("T", CultureInfo.InvariantCulture));
    Console.WriteLine("Time format (short): {0}", d.ToString("t", CultureInfo.InvariantCulture));
    Console.WriteLine("General format (long):  {0}", d.ToString("G", CultureInfo.InvariantCulture));
    Console.WriteLine("General format (short): {0}", d.ToString("g", CultureInfo.InvariantCulture));
}

}

Output:

Date format (long):  Monday, 16 October 2017
Date format (short): 10/16/2017
Full format (long):  Monday, 16 October 2017 20:12:45
Full format (short): Monday, 16 October 2017 20:12
Time format (long):  20:12:45
Time format (short): 20:12
General format (long):  10/16/2017 20:12:45
General format (short): 10/16/2017 20:12

Code on DotNetFiddle.

John Wu
  • 50,556
  • 8
  • 44
  • 80
5

Its more or less the same as en-us but uses as 24 hour time instead of 12 hour am/pm and fills in full MM/DD/YYYY.

var date1 = d.ToString(CultureInfo.InvariantCulture);   // "05/21/2014 22:09:28"
var date2 = d.ToString(new CultureInfo("en-US"));       // "5/21/2014 10:09:28 PM"
thatguy
  • 1,249
  • 9
  • 26
2
  • "O" or "o": yyyy-MM-ddTHH:mm:ss.fffffffzz
  • "R" or "r": ddd, dd MMM yyyy HH:mm:ss
  • "s": yyyy-MM-ddTHH:mm:ss
  • "u": yyyy-MM-dd HH:mm:ssZ

Sources [1]: https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings

Ratatoskr
  • 189
  • 5