Where's the DateTime 'Z' format specifier?

后端 未结 6 1405
抹茶落季
抹茶落季 2020-11-27 06:09

[Update: Format specifiers are not the same thing as format strings; a format specifier is a piece of a custom format string, where a format string is \

相关标签:
6条回答
  • 2020-11-27 06:32

    When you use DateTime you are able to store a date and a time inside a variable.

    The date can be a local time or a UTC time, it depend on you.

    For example, I'm in Italy (+2 UTC)

    var dt1 = new DateTime(2011, 6, 27, 12, 0, 0); // store 2011-06-27 12:00:00
    var dt2 = dt1.ToUniversalTime()  // store 2011-06-27 10:00:00
    

    So, what happen when I print dt1 and dt2 including the timezone?

    dt1.ToString("MM/dd/yyyy hh:mm:ss z") 
    // Compiler alert...
    // Output: 06/27/2011 12:00:00 +2
    
    dt2.ToString("MM/dd/yyyy hh:mm:ss z") 
    // Compiler alert...
    // Output: 06/27/2011 10:00:00 +2
    

    dt1 and dt2 contain only a date and a time information. dt1 and dt2 don't contain the timezone offset.

    So where the "+2" come from if it's not contained in the dt1 and dt2 variable?

    It come from your machine clock setting.

    The compiler is telling you that when you use the 'zzz' format you are writing a string that combine "DATE + TIME" (that are store in dt1 and dt2) + "TIMEZONE OFFSET" (that is not contained in dt1 and dt2 because they are DateTyme type) and it will use the offset of the server machine that it's executing the code.

    The compiler tell you "Warning: the output of your code is dependent on the machine clock offset"

    If i run this code on a server that is positioned in London (+1 UTC) the result will be completly different: instead of "+2" it will write "+1"

    ...
    dt1.ToString("MM/dd/yyyy hh:mm:ss z") 
    // Output: 06/27/2011 12:00:00 +1
    
    dt2.ToString("MM/dd/yyyy hh:mm:ss z") 
    // Output: 06/27/2011 10:00:00 +1
    

    The right solution is to use DateTimeOffset data type in place of DateTime. It's available in sql Server starting from the 2008 version and in the .Net framework starting from the 3.5 version

    0 讨论(0)
  • 2020-11-27 06:34

    Maybe the "K" format specifier would be of some use. This is the only one that seems to mention the use of capital "Z".

    "Z" is kind of a unique case for DateTimes. The literal "Z" is actually part of the ISO 8601 datetime standard for UTC times. When "Z" (Zulu) is tacked on the end of a time, it indicates that that time is UTC, so really the literal Z is part of the time. This probably creates a few problems for the date format library in .NET, since it's actually a literal, rather than a format specifier.

    0 讨论(0)
  • 2020-11-27 06:34

    This page on MSDN lists standard DateTime format strings, uncluding strings using the 'Z'.

    Update: you will need to make sure that the rest of the date string follows the correct pattern as well (you have not supplied an example of what you send it, so it's hard to say whether you did or not). For the UTC format to work it should look like this:

    // yyyy'-'MM'-'dd HH':'mm':'ss'Z'
    DateTime utcTime = DateTime.Parse("2009-05-07 08:17:25Z");
    
    0 讨论(0)
  • 2020-11-27 06:39

    I was dealing with DateTimeOffset and unfortunately the "o" prints out "+0000" not "Z".

    So I ended up with:

    dateTimeOffset.UtcDateTime.ToString("o")
    
    0 讨论(0)
  • 2020-11-27 06:43

    Round tripping dates through strings has always been a pain...but the docs to indicate that the 'o' specifier is the one to use for round tripping which captures the UTC state. When parsed the result will usually have Kind == Utc if the original was UTC. I've found that the best thing to do is always normalize dates to either UTC or local prior to serializing then instruct the parser on which normalization you've chosen.

    DateTime now = DateTime.Now;
    DateTime utcNow = now.ToUniversalTime();
    
    string nowStr = now.ToString( "o" );
    string utcNowStr = utcNow.ToString( "o" );
    
    now = DateTime.Parse( nowStr );
    utcNow = DateTime.Parse( nowStr, null, DateTimeStyles.AdjustToUniversal );
    
    Debug.Assert( now == utcNow );
    
    0 讨论(0)
  • 2020-11-27 06:53
    Label1.Text = dt.ToString("dd MMM yyyy | hh:mm | ff | zzz | zz | z");
    

    will output:

    07 Mai 2009 | 08:16 | 13 | +02:00 | +02 | +2
    

    I'm in Denmark, my Offset from GMT is +2 hours, witch is correct.

    if you need to get the CLIENT Offset, I recommend that you check a little trick that I did. The Page is in a Server in UK where GMT is +00:00 and, as you can see you will get your local GMT Offset.


    Regarding you comment, I did:

    DateTime dt1 = DateTime.Now;
    DateTime dt2 = dt1.ToUniversalTime();
    
    Label1.Text = dt1.ToString("dd MMM yyyy | hh:mm | ff | zzz | zz | z");
    Label2.Text = dt2.ToString("dd MMM yyyy | hh:mm | FF | ZZZ | ZZ | Z");
    

    and I get this:

    07 Mai 2009 | 08:24 | 14 | +02:00 | +02 | +2
    07 Mai 2009 | 06:24 | 14 | ZZZ | ZZ | Z 
    

    I get no Exception, just ... it does nothing with capital Z :(

    I'm sorry, but am I missing something?


    Reading carefully the MSDN on Custom Date and Time Format Strings

    there is no support for uppercase 'Z'.

    0 讨论(0)
提交回复
热议问题