如何回合MySQL的日期时间? [英] How to round a DateTime in MySQL?
本文介绍了如何回合MySQL的日期时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用离散5分钟的分辨率日期时间。我这样做是在C#中,但如何将下面的代码转换为MySQL的?
的DateTime楼(DateTime的日期时间,时间跨度分辨率)
{
返回新的datetime
(
timeSpan.Ticks *
(长)Math.Floor
(
((双)dateTime.Ticks )/
((双)resolution.Ticks)
)
);
}
解决方案
这是一个有点讨厌,当你与时间数据类型做到这一点;一个很好的候选人存储功能。
DATE_SUB(DATE_SUB(时间间隔MOD(分(时间),5)分) ,
间隔秒(时间)秒)
这是容易当你使用UNIXTIME时间戳但是这仅限于1970年 - 2038年的日期范围
FROM_UNIXTIME(UNIX_TIMESTAMP(时间) - MOD(UNIX_TIMESTAMP(次),300))
祝你好运。
I want to discretize the DateTime with the resolution of 5 minutes. I did it in C#, but how to convert the following code to MySQL?
DateTime Floor(DateTime dateTime, TimeSpan resolution)
{
return new DateTime
(
timeSpan.Ticks *
(long) Math.Floor
(
((double)dateTime.Ticks) /
((double)resolution.Ticks)
)
);
}
解决方案
It's a little nasty when you do it with datetime data types; a nice candidate for a stored function.
DATE_SUB(DATE_SUB(time, INTERVAL MOD(MINUTE(time),5) MINUTE ),
INTERVAL SECOND(time) SECOND)
It's easier when you use UNIXTIME timestamps but that's limited to a 1970 - 2038 date range.
FROM_UNIXTIME(UNIX_TIMESTAMP(time) - MOD(UNIX_TIMESTAMP(time),300))
Good luck.
这篇关于如何回合MySQL的日期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文