Reputation: 6349
I am trying to get the date from the present time with a difference of upto 24 hours.
I am using the set hours api accordingly but it is giving me back unexpected results.
let selectedDropdown = 23 //COMMENTED TO SIMULATE SELECT d3.select(".rangeSelect1").node().value;
console.log("Present Date " + new Date());
maxDate = new Date(new Date().setHours(new Date().getHours() + selectedDropdown));
console.log("selectedDropdown " + selectedDropdown);
console.log("Max Date" + maxDate);
THe above picture is the output of the code.
The present date shows as March 29 and by adding 23 hours to it the date is showing as June 04 which actually should be March 30.
Any pointers to fix this or am i doing wrong somewhere?
Upvotes: 0
Views: 161
Reputation: 3408
Your d3 select is coming across as a string not an integer. Try parsing it to an int and then doing math.
Adding as a string you get the invalid results
let selectedDropdown = "23" //COMMENTED TO SIMULATE SELECT d3.select(".rangeSelect1").node().value;
console.log("Present Date " + new Date());
maxDate = new Date(new Date().setHours(new Date().getHours() + selectedDropdown));
console.log("selectedDropdown " + selectedDropdown);
console.log("Max Date" + maxDate);
Parse it as an int first. Wrap the selectedDropdown var in parseInt
let selectedDropdown = "23" //COMMENTED TO SIMULATE SELECT d3.select(".rangeSelect1").node().value;
console.log("Present Date " + new Date());
maxDate = new Date(new Date().setHours(new Date().getHours() + parseInt(selectedDropdown)));
console.log("selectedDropdown " + selectedDropdown);
console.log("Max Date" + maxDate);
Upvotes: 3