InvariantCulture中使用的DateTime格式

时间:2017-10-16 19:58:57

标签: c# datetime-format invariantculture

我必须在Javascript中预先验证一个字符串,该字符串将是c#中的DateTime。 DateTime解析使用InvariantCulture。

有没有人知道为InvariantCulture定义的DateTime格式?

4 个答案:

答案 0 :(得分:18)

从此处汇编信息:https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings

  • 短日期模式(" d"):MM/dd/yyyy
  • 长日期模式(" D"):dddd, dd MMMM yyyy
  • 完整日期短时间(" f"):dddd, dd MMMM yyyy HH:mm
  • 完整日期很长时间(" F"):dddd, dd MMMM yyyy HH:mm:ss
  • 一般日期短时间(" g"):MM/dd/yyyy HH:mm
  • 一般日期长时间(" G"):MM/dd/yyyy HH:mm:ss
  • 月(" M"," m"):MMMM dd
  • 往返(" O"," o"):yyyy-MM-ddTHH:mm:ss.fffffffK
  • RFC1123(" R"," r"):ddd, dd MMM yyyy HH:mm:ss GMT
  • 可排序(" s"):yyyy-MM-ddTHH:mm:ss
  • 短时间(" t"):HH:mm
  • 很长时间(" T"):HH:mm:ss
  • 通用可排序(" u"):yyyy-MM-dd HH:mm:ssZ
  • Universal Full(" U"):dddd, dd MMMM yyyy HH:mm:ss
  • 年月(" Y"," y"):yyyy MMMM

答案 1 :(得分:7)

它很容易测试。

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));
}

}

输出:

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

DotNetFiddle上的代码。

答案 2 :(得分:1)

它或多或少与en-us相同,但用作24小时时间而不是12小时上午/下午并填写完整的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"

答案 3 :(得分:1)

  • " O"或" o":yyyy-MM-ddTHH:mm:ss.fffffffzz
  • " R"或" r":ddd, dd MMM yyyy HH:mm:ss
  • " s":yyyy-MM-ddTHH:mm:ss
  • " u":yyyy-MM-dd HH:mm:ssZ

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