how to convert date with 'T' to/from string in C#

前端 未结 4 1851
无人及你
无人及你 2021-01-04 12:57

I used following functions to convert DateTime from/into string:

DATE_OBJ.ToString(DATE_FORMAT);

DateTime.ParseExact(Date_string,          


        
4条回答
  •  日久生厌
    2021-01-04 13:10

    You cannot do this from DateTime, as DateTime holds no TimeZone info.

    This is close: string.Format("{0:s}", dt) will give 2012-03-20T14:18:25. See: http://www.csharp-examples.net/string-format-datetime/

    You could extend this to: string.Format("{0:s}.{0:fff}", dt), which will give 2012-03-20T14:18:25.000

    But you better have a look at DateTimeOffset: DateTime vs DateTimeOffset

    (Not advisable, but to fake it and still use DateTime: string.Format("{0:s}.{0:fff}+04:00", dt))

提交回复
热议问题