天天看点

c# DateTime 带时区

1.

c# 里的 DateTime.toString 想要带时区打印时有个巨坑,即 “yyyy-MM-dd HH:mm:ss zzz” 中的 "zzz" 不能用来表示时区,需要用 "K"

参考:

c# - Convert DateTime to specific timezone for ToString() - Stack Overflow

Custom date and time format strings | Microsoft Docs

Custom date and time format strings | Microsoft Docs

2. c# 的 DateTime.Parse 函数不支持指定时区读取,需要读取进来之后再转时区。

3.c# 中的 datetime 想要转换为 UTC时区,例如将 “2021-08-24T17:00:00-07:00” 转为 “2021-08-25T00:00:00Z”,可以使用 DateTime.ToUniversalTime(),返回值是一个 DateTime对象

4. format 的时候,大写的 H 是24 进制的小时,小写的 h 是12 进制的小时,不要搞混了

5.像 零点零五分 0:05 表示为 12:05 ,一般是因为采用了12进制小时表示法