I have converted the timestamp 4/1/2021 00:00
into the format Thu Apr 01 2021 00:00:00 GMT-0500 (Central Daylight Time)
as shown in the code below. However, adding 30 days is not giving me appropriate result.
//Handling Date String: 4/1/2021 00:00
function setStart(input) {
if (!(input instanceof Date))
console.log('Handling Date String:' input)
input = new Date(Date.parse(input));
input.setHours(0);
input.setMinutes(0);
input.setSeconds(0);
input.setMilliseconds(0);
start = input;
return start;
}
var initialDate = setStart('4/1/2021 00:00');
console.log("Printing converted date below:");
console.log(setStart('4/1/2021 00:00'));
var date = new Date(); // Now
//date.setDate(date.getDate() 30); // Set now 30 days as the new date
date.setDate(initialDate 30);
console.log("Printing date after adding 30 days below")
console.log(date);
/* var getDaysArray = function(start, end) {
for(var arr=[],dt=new Date(start); dt<=new Date(end); dt.setDate(dt.getDate() 1)){
arr.push(new Date(dt));
}
return arr;
};
var daylist = getDaysArray(new Date("2018-05-01"),new Date("2018-06-01"));
console.log(daylist); */
The browser's console is printing it like the following:
Handling Date String:4/1/2021 00:00
Printing converted date below:
Handling Date String:4/1/2021 00:00
Thu Apr 01 2021 00:00:00 GMT-0500 (Central Daylight Time)
Printing date after adding 30 days below
Invalid Date
What is causing it to print Invalid Date
?
CodePudding user response:
You want to get the date after 30 days from the initialDate, don't you?
refer this
var date = new Date(initialDate); // Now
//date.setDate(date.getDate() 30); // Set now 30 days as the new date
date.setDate(date.getDate() 30);
console.log("Printing date after adding 30 days below")
console.log(date);
CodePudding user response:
You should something like
var date = new Date().getTime(); // Now
const offset = 30*23*3600*1000;
const newDate = new Date(date offset).toLocaleString('en-US', {timeZone: 'CST'}); // Change added
console.log("Printing date after adding 30 days below")
console.log(newDate);