I am creating a date out of strings. I want my date to have the last hour and last minute of the given day. I have a calendar that is populating an input box with a date in the form 01-02-2020
(dd-mm-yyyy).
I want to add hours minutes and seconds to the date to make it look like this string: 2020-02-01 23:59:59
.
I then want to sabtract x number of days from the date I've created to get a startdate.
My issue is that my date values are being somehow converted when I use the date functions. What I am doing is:
enddate = new Date(enddate);
enddate = enddate.setHours(23,59,59);
var startdate = new Date();
startdate.setDate( enddate.getDate() - 5);
I then want to concatenate my two dates to a string. Like ?startdate=2020-01-26 00:00:00&enddate=2020-02-01 23:59:59
. Where the startdate has hours, minutes and seconds in the form 00:00:00
This string is what I ultimately want and it doesn't matter how I get to this start and enddate value. The steps above are just what I've tried. And actually the format of my dates in the the final string doesn't matter as long as it is something that sql can recognize and treat as a date.
How can I accomplish this?
Here is my full code: enddate holds a date value in this form: 01-02-2020 (day month year european style)
datesplit = enddate.split("-");
enddate = new Date(datesplit[2],datesplit[0],datesplit[1]); //mm-dd-yyyy for US format
enddate.setHours(23);
enddate.setMinutes(59);
var startdate = new Date(enddate);
startdate.setDate(startdate.getDate() - daysback);
startdate.setHours(00);
startdate.setMinutes(00);
console.log("startdate and enddate: " + startdate + " - " + enddate)
//startdate and enddate: Tue Jan 28 2020 00:00:00 GMT-0500 (Eastern Standard Time) - Sun Feb 02 2020 23:59:00 GMT-0500 (Eastern Standard Time)
console.log("startdate and enddate date string: " + startdate.toISOString() + " - " + enddate.toISOString());
//startdate and enddate date string: 2020-01-27T05:00:00.000Z - 2020-02-03T04:59:00.000Z
Why the added time in the last console log value when the date is cast to ISO?? That last format is what I want, but value is different.