Reputation: 3717
I've a date variable EndDate
stored in localStorage. Now I want to add exact 24 hours in that. Localstorage value is Sun Jun 09 2019 20:39:44 GMT+0530 (India Standard Time)
var endDate = new Date();
endDate.setDate(new Date(localStorage.getItem("requestDate")).getDate() + 1);
If I run this code it is returning Mon Jun 10 2019 07:58:50 GMT+0530 (India Standard Time). Which is wrong because time is current datetime time.
var endDate = new Date();
endDate.setDate(new Date(localStorage.getItem("requestDate")).getDate() + 1);
// Do your operations
endDate.setTime(new Date(localStorage.getItem("requestDate")).getTime() + 24);
If I run above code it is returning Sun Jun 09 2019 20:39:44 GMT+0530 (India Standard Time), setTime overrides previous date value.
Desired Output is Mon Jun 10 2019 20:39:44 GMT+0530 (India Standard Time)
Upvotes: 0
Views: 2940
Reputation: 1
If you need to add as days to specific date, can use following code.
var startDate = new Date(localStorage.getItem("requestDate"));
var endDate = new Date(startDate);
endDate.setDate(startDate.getDate() + 1);
console.log("Start date: " + startDate);
console.log("End date: " + endDate);
If you need add as hours to specific date, an use following code.
var hours = 10;
var startTime = new Date(localStorage.getItem("requestDate"));
var endTime = new Date(startTime.getTime() + (hours*60*60*1000));
console.log("Start time: " + startTime);
console.log("End time: " + endTime);
Upvotes: 0
Reputation: 495
Try this hope this will work
var endDate = new Date(localStorage.getItem("requestDate"));
endDate.setDate(endDate.getDate() + 1);
Upvotes: 1
Reputation: 1507
Instead of creating a new Date object, you can create a date object from your localStorage and increment on that. To clarify my point check the code below:
var startDate = new Date(localStorage.getItem("requestDate"))
var endDate = new Date(startDate)
endDate.setDate(endDate.getDate() + 1)
console.log('start date', startDate.toString())
console.log('end date', end date.toString())
Hope this helps :)
Upvotes: 0