我需要确定特定的日期时间格式

本文关键字:日期 时间 格式 | 更新日期: 2023-09-27 18:33:45

以下日期时间字符串采用什么格式?我需要用 C# 阅读它。

2010-09-29T02:40:00.2291503+05:30

我需要确定特定的日期时间格式

您可以使用DateTime.Parse(string)

分析字符串

DateTime.Parse("2010-09-29T02:40:00.2291503+05:30");

这是带有往返的日期时间

"

O"或"o"标准格式说明符(以及 "yyyy'-'MM'-'dd'T'HH':'mm':'ss'。"fffffffK"自定义格式字符串)采用 ISO 8601 表示时区的三种方式的优势 用于保留日期时间值的 Kind 属性的信息:

  • 日期时间种类的时区组件。本地日期和时间值是 UTC 的偏移量(例如,+01:00、-07:00)。都 日期时间偏移量值也以这种格式表示。

  • DateTimeKind.Utc 日期和时间值的时区组件使用"Z"(代表零偏移量)来表示 UTC。

  • 日期时间种类。未指定的日期和时间值没有时区信息。


下面是使用 DateTime.Kind 的示例:

using System;
public class Example
{
   public static void Main()
   {
       DateTime dat = new DateTime(2009, 6, 15, 13, 45, 30, 
                                   DateTimeKind.Unspecified);
       Console.WriteLine("{0} ({1}) --> {0:O}", dat, dat.Kind); 
       DateTime uDat = new DateTime(2009, 6, 15, 13, 45, 30, 
                                    DateTimeKind.Utc);
       Console.WriteLine("{0} ({1}) --> {0:O}", uDat, uDat.Kind);
       DateTime lDat = new DateTime(2009, 6, 15, 13, 45, 30, 
                                    DateTimeKind.Local);
       Console.WriteLine("{0} ({1}) --> {0:O}'n", lDat, lDat.Kind);
       DateTimeOffset dto = new DateTimeOffset(lDat);
       Console.WriteLine("{0} --> {0:O}", dto);
   }
}
// The example displays the following output:
//    6/15/2009 1:45:30 PM (Unspecified) --> 2009-06-15T13:45:30.0000000
//    6/15/2009 1:45:30 PM (Utc) --> 2009-06-15T13:45:30.0000000Z
//    6/15/2009 1:45:30 PM (Local) --> 2009-06-15T13:45:30.0000000-07:00
//    
//    6/15/2009 1:45:30 PM -07:00 --> 2009-06-15T13:45:30.0000000-07:00