使用C#ssTZD格式:如何YYYY-MM-DDTHH得到DateTime.Now():毫米 [英] How to get DateTime.Now() in YYYY-MM-DDThh:mm:ssTZD format using C#

查看:1328
本文介绍了使用C#ssTZD格式:如何YYYY-MM-DDTHH得到DateTime.Now():毫米的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
日期时间为字符串时区

这是我想在网站地图使用W3C标准的日期和时间格式之一。这个日期时间标准是:

This is one of the W3C standard date time format that I want to use in sitemap. This DateTime standard is:

完成日期以及小时,分钟和秒:
YYYY-MM-DDTHH:MM:ssTZD哪里TZD =时区指示符(Z或+ HH:MM或-hh:毫米)(如1997-07-16T19:20:30 + 01:00)

Complete date plus hours, minutes and seconds: YYYY-MM-DDThh:mm:ssTZD where TZD = time zone designator (Z or +hh:mm or -hh:mm) (eg 1997-07-16T19:20:30+01:00)

我使用下面的代码来获得该格式的当前日期时间:

I am using the following code to get the current DateTime in that format:

DateTime.Now.ToString("yyyy-MM-ddThh:mm:ssTZD");



不过,这给:2011-08-10T02:27:20TZD

But this gives: 2011-08-10T02:27:20TZD

显然,DateTime.Now不会在参数识别TZD。请帮忙。我怎样才能得到当前日期时间的这种格式?

Apparently the DateTime.Now doesn't recognize "TZD" in the parameter. Please help. How can I get the current DateTime in this format?

推荐答案

使用 ZZZ 格式说明,以获得时区的小时和分钟偏移量。你也想使用 HH 格式说明,以获得以24小时格式的时间。

Use the zzz format specifier to get the timezone offset as hours and minutes. You also want to use the HH format specifier to get the hours in 24 hour format.

DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz")

结果:

2011-08-09T23:49:58+02:00

一些区域性设置使用期限,而不是冒号的时间,所以您可能需要使用文字冒号,而不是时间分隔符:

Some culture settings uses periods instead of colons for time, so you might want to use literal colons instead of time separators:

DateTime.Now.ToString("yyyy-MM-ddTHH':'mm':'sszzz")

自定义日期和时间格式弦乐

这篇关于使用C#ssTZD格式:如何YYYY-MM-DDTHH得到DateTime.Now():毫米的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆