简体   繁体   English

如何从明天nodejs获取unix时间戳

[英]How to get unix timestamp from tomorrow nodejs

I want to get Unix timestamp (time in seconds) from tomorrow. 我想从明天开始获取Unix时间戳(以秒为单位的时间)。

I have tried the following with no success: 我试过以下但没有成功:

var d = new Date();
d.setDate(d.getDay() - 1);
d.setHours(0, 0, 0);
d.setMilliseconds(0);
console.log(d/1000|0)

How would I fix the above? 我该如何解决上述问题?

Just modified your code and it works fine 只是修改了你的代码,它工作正常

var d = new Date();
d.setDate(d.getDate() + 1);
d.setHours(0, 0, 0);
d.setMilliseconds(0);
console.log(d)
>> Sun Apr 21 2019 00:00:00 GMT+0530 (India Standard Time)

Hope this will work for you 希望这对你有用

This should do it. 这应该做到这一点。

Copied directly from https://javascript.info/task/get-seconds-to-tomorrow 直接从https://javascript.info/task/get-seconds-to-tomorrow复制

 function getSecondsToTomorrow() { let now = new Date(); // tomorrow date let tomorrow = new Date(now.getFullYear(), now.getMonth(), now.getDate()+1); let diff = tomorrow - now; // difference in ms return Math.round(diff / 1000); // convert to seconds } console.log(getSecondsToTomorrow()); 

you could use a third party library like moment js which makes your life alot easier 你可以使用第三方库,比如片刻js,让你的生活更轻松

momentjs momentjs

You can use a unix timestamp and add 24*60*60*1000 (same as 86400000 ) to the current time's timestamp. 您可以使用unix时间戳并将24*60*60*1000 (与86400000相同)添加到当前时间的时间戳。 You can then pass that to new Date() like this: 然后,您可以将其传递给new Date()如下所示:

  • 24 = hours 24 =小时
  • 60 = minutes 60 =分钟
  • 60 = seconds 60 =秒
  • 1000 = converts the result to milliseconds 1000 =将结果转换为毫秒

 // Current timestamp const now = Date.now() // Get 24 hours from now const next = new Date(now + (24*60*60*1000)) // Create tomorrow's date const t = new Date(next.getFullYear(), next.getMonth(), next.getDate()) // Subtract the two and divide by 1000 console.log(Math.round((t.getTime() - now) / 1000), 'seconds until tomorrow') 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM