3

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;
  • 1
    Possible duplicate of [Adding hours to Javascript Date object?](http://stackoverflow.com/questions/1050720/adding-hours-to-javascript-date-object) – nicecatch Oct 28 '16 at 11:57
  • If you need to more lots of date manipulations momentjs is highly recommended – mcfedr Oct 29 '16 at 15:52

2 Answers2

3
var myDate = new Date();
myDate.setHours(myDate.getHours()+24);
return myDate;
nicecatch
  • 1,687
  • 2
  • 21
  • 38
  • 1
    this one is much better than the other one – Beginner Oct 28 '16 at 11:36
  • 1
    Thanks for reply, I run the above code and i got the output as follows Fri Oct 28 2016 17:23:52 GMT+0530 (India Standard Time) Sat Oct 29 2016 17:23:52 GMT+0530 (India Standard Time) But here i have to convert this string time to unix timestamp again. And this will increase my little more processing time. Is there is any direct function where i will enter only the added extra time (current unixtime+24hr) and it will return me the final answer in unix timestamp. $newUnixTime=functionName('+1 day', Date.now()); – Chandrakar Ramkishan Oct 28 '16 at 12:03
  • There's no such thing like '+1 day' in standard javascript library. You can use external libraries like momentjs.com to be able to do moment().add(1, 'days') – nicecatch Oct 28 '16 at 12:18
0

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
jakub.g
  • 38,512
  • 12
  • 92
  • 130