How can I format DateTime to web UTC format?

前端 未结 8 1900
梦毁少年i
梦毁少年i 2020-11-29 22:45

I have a DateTime which I want to format to \"2009-09-01T00:00:00.000Z\", but the following code gives me \"2009-09-01T00:00:00.000+01:00\" (both l

相关标签:
8条回答
  • 2020-11-29 23:09

    Some people have pointed out that ‘ToUniversalTime’ is somewhat unsafe in that it can cause unintended incorrect time dispalys. Expanding on that I’m providing a more detailed example of a solution. The sample here creates an extension to the DateTime object that safely returns a UTC DateTime where you can use ToString as desired….

    class Program
    {
        static void Main(string[] args)
        {
            DateTime dUtc = new DateTime(2016, 6, 1, 3, 17, 0, 0, DateTimeKind.Utc);
            DateTime dUnspecified = new DateTime(2016, 6, 1, 3, 17, 0, 0, DateTimeKind.Unspecified);
    
            //Sample of an unintended mangle:
            //Prints "2016-06-01 10:17:00Z"
            Console.WriteLine(dUnspecified.ToUniversalTime().ToString("u"));
    
            //Prints "2016 - 06 - 01 03:17:00Z"
            Console.WriteLine(dUtc.SafeUniversal().ToString("u"));
    
            //Prints "2016 - 06 - 01 03:17:00Z"
            Console.WriteLine(dUnspecified.SafeUniversal().ToString("u"));
        }
    }
    
    public static class ConvertExtensions
    {
        public static DateTime SafeUniversal(this DateTime inTime)
        {
            return (DateTimeKind.Unspecified == inTime.Kind)
                ? new DateTime(inTime.Ticks, DateTimeKind.Utc)
                : inTime.ToUniversalTime();
        }
    }
    
    0 讨论(0)
  • 2020-11-29 23:12

    The best format to use is "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffK".

    The last K on string will be changed to 'Z' if the date is UTC or with timezone (+-hh:mm) if is local. (http://msdn.microsoft.com/en-us/library/8kb3ffffd4.aspx)

    As LukeH said, is good to use the ToUniversalTime if you want that all the dates will be UTC.

    The final code is:

    string foo = yourDateTime.ToUniversalTime()
                             .ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffK");
    
    0 讨论(0)
  • 2020-11-29 23:20

    Try this:

    DateTime date = DateTime.ParseExact(
        "Tue, 1 Jan 2008 00:00:00 UTC", 
        "ffffd, d MMM yyyy HH:mm:ss UTC", 
        CultureInfo.InvariantCulture);
    

    Previously asked question

    0 讨论(0)
  • 2020-11-29 23:21
    string foo = yourDateTime.ToUniversalTime()
                             .ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'");
    
    0 讨论(0)
  • 2020-11-29 23:27

    You want to use DateTimeOffset class.

    var date = new DateTimeOffset(2009, 9, 1, 0, 0, 0, 0, new TimeSpan(0L));
    var stringDate = date.ToString("u");
    

    sorry I missed your original formatting with the miliseconds

    var stringDate = date.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'");
    
    0 讨论(0)
  • 2020-11-29 23:28

    This code is working for me:

    var datetime = new DateTime(2017, 10, 27, 14, 45, 53, 175, DateTimeKind.Local);
    var text = datetime.ToString("o");
    Console.WriteLine(text);
    --  2017-10-27T14:45:53.1750000+03:00
    
    // datetime from string
    var newDate = DateTime.ParseExact(text, "o", null);
    
    0 讨论(0)
提交回复
热议问题