• 欢迎订阅我的Youtube频道
  • 欢迎加入我的电报群

How to get UTC 0 o’clock of current date in javascript

软件开发 fennng 3个月前 (03-14) 171次浏览 0个评论
It’s common that we need to get the UTC date of today. With Javascript, you can get the current day easily with new Date() or Date.now().
But sometimes, we would like to treat any time in the same day the same way, which means we don’t want to know the hour, minute, second of the day. We just care about the day.
You can always convert the date to string and ignore the hours etc. which is inefficient. It’s also problematical if we need to save the date into db. So convert to string then convert back to time stamp is necessary but make it more inefficient.
Today I am showing a pure mathematical way.
TodayUTCZeroOClock = Now – Now%DayInMiniSeconds
Example:
a = Date.now()
1552561275091
day = 1000 * 60 * 60 * 24
86400000
a%day
39675091
b = a – a%day
1552521600000
new Date(b)
Thu Mar 14 2019 13:00:00 GMT+1300 (New Zealand Daylight Time)
Note that 13 O’clock is UTC 0 0’clock

峰享技术 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:How to get UTC 0 o’clock of current date in javascript
喜欢 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址