将原始字符串日期解析为实际的Datetime对象

时间:2019-07-15 11:37:58

标签: c# .net datetime timezone

我有一个日期字符串为“ CET July July 1pm”,我想将其转换为系统时间为IST的正确datetime对象。

如何将其解析为正确的日期时间对象?

1 个答案:

答案 0 :(得分:0)

https://docs.microsoft.com/en-us/dotnet/api/system.datetime.parse?view=netframework-4.8

public class Example
{
   public static void Main()
   {
      string[] dateStrings = {"2008-05-01T07:34:42-5:00", 
                              "2008-05-01 7:34:42Z", 
                              "Thu, 01 May 2008 07:34:42 GMT"};
      foreach (string dateString in dateStrings)
      {
         DateTime convertedDate = DateTime.Parse(dateString);
         Console.WriteLine($"Converted {dateString} to {convertedDate.Kind} time {convertedDate}");
      }                              
   }
}