简体   繁体   中英

How to add 24hr in current unix timestamp in NodeJs or in javascript?

I would like to add 24 hours to the unix timestamp for now in nodejs or in javascript . I also would like to know is there is any direct function or property in Date DOM object. I found the relevent function in PHP. This code will returns new unix time after adding 24hrs in current unix timestamp.

$currentUnixTime=time();
$newUnixTime=strtotime('+1 day', $currentUnixTime);
return newUnixTime;
var myDate = new Date();
myDate.setHours(myDate.getHours()+24);
return myDate;

If you specifically want to add one day, you should use momentjs library, which is available both for frontent and nodejs.

var now = new Date()
var tomorrow = moment(now).add(1, 'day');

This is more robust than adding 24 hours because it takes into account DST changes. For that sole reason you should avoid direct Date manipulation in JS in most of the cases.

http://momentjs.com/docs/#/manipulating/add/

Special considerations for months and years

If the day of the month on the original date is greater than the number of days in the final month, the day of the month will change to the last day in the final month.

 moment([2010, 0, 31]); // January 31 moment([2010, 0, 31]).add(1, 'months'); // February 28 

There are also special considerations to keep in mind when adding time that crosses over daylight saving time. If you are adding years, months, weeks, or days, the original hour will always match the added hour.

 var m = moment(new Date(2011, 2, 12, 5, 0, 0)); // the day before DST in the US m.hours(); // 5 m.add(1, 'days').hours(); // 5 

If you are adding hours, minutes, seconds, or milliseconds, the assumption is that you want precision to the hour, and will result in a different hour.

 var m = moment(new Date(2011, 2, 12, 5, 0, 0)); // the day before DST in the US m.hours(); // 5 m.add(24, 'hours').hours(); // 6 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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