cup_of
cup_of

Reputation: 6687

Increment date by one day to a set epoch date

I would like to increment a (epoch) date by one day.

So far I have:

let date = "1535162451650"; // August 24 2018
console.log(new Date(parseInt(date, 10)).getDate() + 1);

This spits out 25 so I am on the right track. How would I convert it back to a Date object?

This is going to be in this map function:

return data.map(t => ({
  id: t.id,
  start_date: new Date(parseInt(t.date_created, 10)),
  duration: // here, taking the above start date and adding one day
  )
}));

Upvotes: 0

Views: 626

Answers (3)

user2575725
user2575725

Reputation:

How about this?

var options = {
  id: t.id,
  start_date: new Date(parseInt(t.date_created, 10))
};
options.duration = new Date(options.start_date.getTime());
options.duration.setDate(options.duration.getDate() + 1);
return data.map(t => (options));

Upvotes: 1

Sandip Nirmal
Sandip Nirmal

Reputation: 2459

I think you can add day in milliseconds to achieve this.

let date = "1535162451650"; // August 24 2018
console.log(new Date(parseInt(date, 10)).getDate() + 1);

let nextDay = +date + (24 * 60 * 60 * 1000) // 1 day in millisecond

nextDay = new Date(nextDay)
console.log(nextDay)

You can also use momentjs in following way:

var date = 1535162451650

date = moment(abc)
console.log('date', date.format('DD MM YYYY'))

date = date.add(1, 'day')
console.log('date', date.format('DD MM YYYY'))

Upvotes: 1

cup_of
cup_of

Reputation: 6687

I think I figured it out. Looks ugly but seems to work

let date = "1535162451650";
console.log(new Date (new Date(parseInt(date, 10)).setDate(new Date(parseInt(date, 10)).getDate() + 1)));
// gives me aug 25 2018

Is there a cleaner way to do this? haha

Upvotes: -1

Related Questions