如何表示各个时区的时间DateTime.ToString()
使用sqlite进行时间的插入的时候,使用了
DateTime.ToString("s") //s: 2008-06-15T21:15:07
插入到数据库之后,发现时间被加了8个小时
找了半天资料,才找到原因
This profile defines two ways of handling time zone offsets:
有两种方式处理时区的时差
Times are expressed in UTC (Coordinated Universal Time), with a special UTC designator ("Z").
如果是UTC时间的话,后面需要加Z表示。1994-11-05T13:15:30Z
Times are expressed in local time, together with a time zone offset in hours and minutes.
A time zone offset of "+hh:mm" indicates that the date/time uses a local time zone which is "hh" hours and "mm" minutes ahead of UTC.
A time zone offset of "-hh:mm" indicates that the date/time uses a local time zone which is "hh" hours and "mm" minutes behind UTC.
其它的时间,在年月日时分秒后面,通过+-时差来表示。
如果时间比UTC时间快的话,就用+hh:mm表示,后面的时间表示相差的时间。
如果时间比UTC时间慢的话,就用-hh:mm表示,后面的时间表示相差的时间。
+hh:mm和-hh:mm实际上表示的是时区,+08:00表示的是东八区,-05:00表示的是西五区
有两种方式来说明时间
1994-11-05T08:15:30-05:00 corresponds to November 5, 1994, 8:15:30 am, US Eastern Standard Time.
//时间是1994-11-05 08:15:30 表示的是西五区的时间,比UTC时间慢5个小时 换算成UTC时间的话,需要加上5个小时,就是1994-11-05 13:15:30
1994-11-05T16:15:30+08:00 表示的是东八区的时间,比UTC时间快8个小时 换算成UTC时间的话,需要减去8个小时,就是1994-11-05 13:15:30
1994-11-05T13:15:30Z corresponds to the same instant.//很明显是UTC时间
总结:
DateTime.ToString("s") //s: 2008-06-15T21:15:07
这个时间在存入SQLite数据库的时候,被当做UTC时间了,因为我的电脑里面设置的时区,是东八区的北京时间,所以被加上了8个小时存储
解决方法:
DateTime.ToString("yyyy-MM-dd HH:mm:ss") 使用这个,来存储就没有问题。
或者可以考虑设置SQLiteConnecttion.DateTimeKind
参考
Date and Time Formats
http://www.w3.org/TR/NOTE-datetime
DateTime.ToString Method (String)
http://msdn.microsoft.com/en-us/library/zdtaw1bw(v=vs.110).aspx
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了