-1

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.

mo_maat
  • 2,110
  • 12
  • 44
  • 72
  • You do not appear to have tried to put the two dates together into 1 string. – Scott Hunter Feb 01 '20 at 22:32
  • Please post enough code that I can debug it. – xsoftie Feb 01 '20 at 22:33
  • I found the answer to my question here: https://stackoverflow.com/questions/17545708/parse-date-without-timezone-javascript/17545854 with wawka's response based on converting the time using date.getTimezoneOffset(). – mo_maat Feb 03 '20 at 03:23

2 Answers2

0
var st = "01-02-2020";
var pattern = /(\d{2})\-(\d{2})\-(\d{4})/;
var dt = new Date(st.replace(pattern,'$3-$2-$1'));

enddate = new Date(dt);
enddate.setHours(23,59,59);

var startdate = new Date(dt);
startdate.setHours(00,00,00);
startdate.setDate( startdate.getDate() - 5);

function js_yyyy_mm_dd_hh_mm_ss (mydate) {
  now = new Date();
  year = "" + mydate.getFullYear();
  month = "" + (mydate.getMonth() + 1); if (month.length == 1) { month = "0" + month; }
  day = "" + mydate.getDate(); if (day.length == 1) { day = "0" + day; }
  hour = "" + mydate.getHours(); if (hour.length == 1) { hour = "0" + hour; }
  minute = "" + mydate.getMinutes(); if (minute.length == 1) { minute = "0" + minute; }
  second = "" + mydate.getSeconds(); if (second.length == 1) { second = "0" + second; }
  return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
}
var query = '?startdate='+js_yyyy_mm_dd_hh_mm_ss(startdate)+'&enddate='+js_yyyy_mm_dd_hh_mm_ss(enddate);
alert(query);
db1975
  • 775
  • 3
  • 9
  • Thanks. I see what you are doing. The final result is slightly off but I can fix it. Is this the only/best way to accomplish what I am trying to do? I thought the built in methods on dates could handle these conversions. – mo_maat Feb 01 '20 at 23:12
0

Well the problem basically was that you assign the same variable to different uses. I separate it (by adding 1 to the second one) and that solve it:

const enddate = '02-01-2020';
const daysback = 5;
 datesplit = enddate.split("-");
        enddate1 = new Date(datesplit[2],datesplit[0],datesplit[1]); //mm-dd-yyyy for US format 
        enddate1.setHours(23);
        enddate1.setMinutes(59);
        var startdate = new Date(enddate1);
        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() + " - " + enddate1.toISOString());
        //startdate and enddate date string: 2020-01-27T05:00:00.000Z - 2020-02-03T04:59:00.000Z
A. Meshu
  • 4,053
  • 2
  • 20
  • 34