C# 关于DateTime类型 精确到毫秒

C# 关于DateTime类型 精确到毫秒,第1张

datetime包含毫秒,要格式化输出,用fff

DateTime t = DateTime.Now

Console.WriteLine(t.ToString("yyyy-MM-dd hh:mm:ss fff"))

注:mysql里面的datetime类型的精确度是可以到1/ 10 ^ 6 秒的,某些客户端(如navicat for mysql)的显示经常只能看到精确到秒,其实是设计表的时候的配置问题。

扩展资料:

mysql中DateTime和Timestamp

DateTime

1、8个字节储存(8 bytes storage)

2、实际格式储存(Just stores what you have stored and retrieves the same thing which you have stored.)

3、与时区无关(It has nothing to deal with the TIMEZONE and Conversion.)

4、存储的时间范围为:'1000-01-01 00:00:00.000000' 到 '9999-12-31 23:59:59.999999'

Timestamp

1、4个字节储存(Time stamp value is stored in 4 bytes)

2、值以UTC格式保存( it stores the number of milliseconds)

3、时区转化 ,存储时对当前的时区进行转换,检索时再转换回当前的时区。

4、存储的时间范围为:'1970-01-01 00:00:01.000000' 到 '2038-01-19 03:14:07.999999'

在mysql中,这种计算可用TIMESTAMPDIFF函数来解决,但是解决过程中需要将数据多次加工。

1、创建测试表及插入测试数据:

create table test

(time1 datetime,

time2 datetime)

insert into test values ('2015-04-03 17:01:09','2015-06-03 22:09:30')

2、目前要结算time2和time1的时间差,用如下语句:

select round(TIMESTAMPDIFF(second,time1,time2)/3600,2) from test

结果如图:

解读:

首先,先用

select TIMESTAMPDIFF(second,time1,time2) from test

来计算两个时间之间的秒数差。

然后,得到的结果除以3600,即为这两个时间之间的小时数。

最后因为要求保留2位小数,则通过round函数,四舍五入,取出2位小数即可。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/7582768.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-07
下一篇 2023-04-07

发表评论

登录后才能评论

评论列表(0条)

保存