加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 日期格式yyyy-MM-ddTHH:mm:ssZ

发布时间:2020-12-16 01:20:44 所属栏目:百科 来源:网络整理
导读:我认为这应该很简单,但无法得到它:(. 在这种格式中,Z是时区. ?T是长时间模式 ?除了使用之外,我怎么能得到这种格式的日期 DateTime dt = DateTime.Now;Console.WriteLine(dt.ToString("yyyy-MM-ddTHH:mm:ssZ")); 在C#中 解决方法 使用UTC ISO 8601(MSDN datet
我认为这应该很简单,但无法得到它:(.
在这种格式中,Z是时区.
?T是长时间模式
?除了使用之外,我怎么能得到这种格式的日期

DateTime dt = DateTime.Now;
Console.WriteLine(dt.ToString("yyyy-MM-ddTHH:mm:ssZ"));

在C#中

解决方法

使用UTC

ISO 8601(MSDN datetime formats)

Console.WriteLine(DateTime.UtcNow.ToString("s") + "Z");

2009-11-13T10:39:35Z

Z就在那里因为

If the time is in UTC,add a ‘Z’
directly after the time without a
space. ‘Z’ is the zone designator for
the zero UTC offset. “09:30 UTC” is
therefore represented as “09:30Z” or
“0930Z”. “14:45:15 UTC” would be
“14:45:15Z” or “144515Z”.

如果要包含偏移量

int hours = TimeZoneInfo.Local.BaseUtcOffset.Hours;
string offset = string.Format("{0}{1}",((hours >0)? "+" :""),hours.ToString("00"));
string isoformat = DateTime.Now.ToString("s") + offset;
Console.WriteLine(isoformat);

有两点需要注意:或者 – 在时间之后需要 – 但显然不会显示正数.根据维基百科,偏移可以是hh格式或hh:mm.我一直待上几个小时.

据我所知,RFC1123(HTTP日期,“你”格式化程序)并不意味着给出时区偏移.所有时间都是GMT / UTC.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读