is there a format in ToString() method of DateTime to convert the time Zone to say UTC ?
在DateTime的ToString()方法中有一种格式将时区转换为UTC吗?
I know I can programatically first convert the DateTime to UTC and then call ToString, but I have a UI where the user can specify format, can they at the same time convert to UTC ?
我知道我可以以编程方式首先将DateTime转换为UTC然后调用ToString,但我有一个用户可以指定格式的UI,它们可以同时转换为UTC吗?
3 个解决方案
#1
The .ToString("u") will format as UTC but not convert. This code below will convert and present the Date and Time in UTC format:
.ToString(“u”)格式为UTC但不转换。以下代码将转换并以UTC格式显示日期和时间:
System.TimeZone.CurrentTimeZone.ToUniversalTime(Date.Now).ToString("u")
or
DateTime.Now.ToUniversalTime().ToString("u")
other formats can be found here
其他格式可以在这里找到
#2
Not built in, but you can create your own formatter (google IFormatProvider)
不是内置的,但你可以创建自己的格式化程序(谷歌IFormatProvider)
#3
No, you'll first have to convert the DateTime to the desired time zone.
不,您首先必须将DateTime转换为所需的时区。
#1
The .ToString("u") will format as UTC but not convert. This code below will convert and present the Date and Time in UTC format:
.ToString(“u”)格式为UTC但不转换。以下代码将转换并以UTC格式显示日期和时间:
System.TimeZone.CurrentTimeZone.ToUniversalTime(Date.Now).ToString("u")
or
DateTime.Now.ToUniversalTime().ToString("u")
other formats can be found here
其他格式可以在这里找到
#2
Not built in, but you can create your own formatter (google IFormatProvider)
不是内置的,但你可以创建自己的格式化程序(谷歌IFormatProvider)
#3
No, you'll first have to convert the DateTime to the desired time zone.
不,您首先必须将DateTime转换为所需的时区。