Converting a String to DateTime

后端 未结 17 2574
南方客
南方客 2020-11-21 06:12

How do you convert a string such as 2009-05-08 14:40:52,531 into a DateTime?

17条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-21 06:43

    I just found an elegant way:

    Convert.ChangeType("2020-12-31", typeof(DateTime));
    
    Convert.ChangeType("2020/12/31", typeof(DateTime));
    
    Convert.ChangeType("2020-01-01 16:00:30", typeof(DateTime));
    
    Convert.ChangeType("2020/12/31 16:00:30", typeof(DateTime), System.Globalization.CultureInfo.GetCultureInfo("en-GB"));
    
    Convert.ChangeType("11/شعبان/1437", typeof(DateTime), System.Globalization.CultureInfo.GetCultureInfo("ar-SA"));
    
    Convert.ChangeType("2020-02-11T16:54:51.466+03:00", typeof(DateTime)); // format: "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffzzz"
    

提交回复
热议问题