Given a DateTime object, how do I get an ISO 8601 date in string format?

后端 未结 18 2216
暖寄归人
暖寄归人 2020-11-22 02:15

Given:

DateTime.UtcNow

How do I get a string which represents the same value in an ISO 8601-compliant format?

Note that ISO 8601 de

18条回答
  •  粉色の甜心
    2020-11-22 02:28

    Surprised that no one suggested it:

    System.DateTime.UtcNow.ToString("u").Replace(' ','T')
    
    # Using PowerShell Core to demo
    
    # Lowercase "u" format
    [System.DateTime]::UtcNow.ToString("u")
    > 2020-02-06 01:00:32Z
    
    # Lowercase "u" format with replacement
    [System.DateTime]::UtcNow.ToString("u").Replace(' ','T')
    > 2020-02-06T01:00:32Z
    
    

    The UniversalSortableDateTimePattern gets you almost all the way to what you want (which is more an RFC 3339 representation).


    Added: I decided to use the benchmarks that were in answer https://stackoverflow.com/a/43793679/653058 to compare how this performs.

    tl:dr; it's at the expensive end but still just a little over half a millisecond on my crappy old laptop :-)

    Implementation:

    [Benchmark]
    public string ReplaceU()
    {
       var text = dateTime.ToUniversalTime().ToString("u").Replace(' ', 'T');
       return text;
    }
    

    Results:

    // * Summary *
    
    BenchmarkDotNet=v0.11.5, OS=Windows 10.0.19002
    Intel Xeon CPU E3-1245 v3 3.40GHz, 1 CPU, 8 logical and 4 physical cores
    .NET Core SDK=3.0.100
      [Host]     : .NET Core 3.0.0 (CoreCLR 4.700.19.46205, CoreFX 4.700.19.46214), 64bit RyuJIT
      DefaultJob : .NET Core 3.0.0 (CoreCLR 4.700.19.46205, CoreFX 4.700.19.46214), 64bit RyuJIT
    
    
    |               Method |     Mean |     Error |    StdDev |
    |--------------------- |---------:|----------:|----------:|
    |           CustomDev1 | 562.4 ns | 11.135 ns | 10.936 ns |
    |           CustomDev2 | 525.3 ns |  3.322 ns |  3.107 ns |
    |     CustomDev2WithMS | 609.9 ns |  9.427 ns |  8.356 ns |
    |              FormatO | 356.6 ns |  6.008 ns |  5.620 ns |
    |              FormatS | 589.3 ns |  7.012 ns |  6.216 ns |
    |       FormatS_Verify | 599.8 ns | 12.054 ns | 11.275 ns |
    |        CustomFormatK | 549.3 ns |  4.911 ns |  4.594 ns |
    | CustomFormatK_Verify | 539.9 ns |  2.917 ns |  2.436 ns |
    |             ReplaceU | 615.5 ns | 12.313 ns | 11.517 ns |
    
    // * Hints *
    Outliers
      BenchmarkDateTimeFormat.CustomDev2WithMS: Default     -> 1 outlier  was  removed (668.16 ns)
      BenchmarkDateTimeFormat.FormatS: Default              -> 1 outlier  was  removed (621.28 ns)
      BenchmarkDateTimeFormat.CustomFormatK: Default        -> 1 outlier  was  detected (542.55 ns)
      BenchmarkDateTimeFormat.CustomFormatK_Verify: Default -> 2 outliers were removed (557.07 ns, 560.95 ns)
    
    // * Legends *
      Mean   : Arithmetic mean of all measurements
      Error  : Half of 99.9% confidence interval
      StdDev : Standard deviation of all measurements
      1 ns   : 1 Nanosecond (0.000000001 sec)
    
    // ***** BenchmarkRunner: End *****
    
    

提交回复
热议问题