简体   繁体   English

C#日期时间格式

[英]C# date time format

How to get date time in 如何获取日期时间

2017-06-15T14:20:30+02:00 2017-06-15T14:20:30 + 02:00

format from 格式自

DateTime.utc.now

It is an ISO 8601 date format. 这是ISO 8601日期格式。 I tried with 我尝试过

DateTime.UtcNow.ToString(@"yyyy-MM-ddTHH\:mm\:ss") + DateTimeOffset.UtcNow.ToString(@"zzz");

But the date I am getting is 但是我得到的日期是

2017-10-20-T09:29:20+00:00 2017年10月20日-T09:29:20 + 00:00

The application is hosted in Azure. 该应用程序托管在Azure中。

I think you are misunderstanding ISO8601. 我认为您误解了ISO8601。 For UtcNow , the offset suffix will always be +00:00. 对于UtcNow ,偏移后缀将始终为+00:00。 The format shows local time, and the suffix means how much the local time is offset relatively to UTC time. 格式显示本地时间,后缀表示本地时间相对于UTC时间偏移了多少。

UtcNow is the current time in the UTC zone, not the local zone - ergo, its offset to UTC is 0. UtcNow是UTC区域中的当前时间,而不是本地区域-ergo,其与UTC的偏移量是0。

DateTime.UtcNow.ToString(@"yyyy-MM-ddTHH:mm:ss") + DateTimeOffset.Now.Offset.ToString();

This is how this works: 这是这样的:

EX 1: 例1:

DateTimeOffset localTime = DateTimeOffset.Now;
DateTimeOffset utcTime = DateTimeOffset.UtcNow;

Console.WriteLine("Local Time:          {0}", localTime.ToString("T"));
Console.WriteLine("Difference from UTC: {0}", localTime.Offset.ToString());
Console.WriteLine("UTC:                 {0}", utcTime.ToString("T"));
// If run on a particular date at 1:19 PM, the example produces
// the following output:
//    Local Time:          1:19:43 PM
//    Difference from UTC: -07:00:00
//    UTC:                 8:19:43 PM  

Ex 2: 例2:

var dt = new DateTime(2010, 1, 1, 1, 1, 1, DateTimeKind.Utc);
        string s = dt.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss \"GMT\"zzz");
        Console.WriteLine(s);

试试这个。

    DateTime.UtcNow.ToString(@"yyyy-MM-ddTHH\:mm\:sszzz")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM