如何表示各个时区的时间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

原文地址:https://www.cnblogs.com/chucklu/p/4105470.html