0

I have run into a really weird problem.

I have two Javascript string variables which get their data from two text boxes.

The third string is pulled from the table cell. However, when it is converted to a Javascript Date() its date changes. I have no idea why this happening.

I will comment by code to help explain what is going on.

//get the text from the first textbox (from date)
var valueFrom = document.getElementById('selected-submittedDate-from').value;
                //convert the string into the right format              
                var formatDateString = function (unformatted) {
                    var parts = unformatted.split('-');
                    return parts[1] + '/' + parts[2] + '/' + parts[0];
                };
                var formattedDateFrom = formatDateString(valueFrom);

    //get the text from the second textbox (to date)
//convert the string into the right format
                var valueTo = document.getElementById('selected-submittedDate-to').value;
                var formatDateStringTo = function (unformatted) {
                    var parts = unformatted.split('-');
                    return parts[1] + '/' + parts[2] + '/' + parts[0];
                };
                var formattedDateTo = formatDateStringTo(valueTo);

//just make some new variables and set the formatted dates to them
                // date from, date to
                var dateFrom = formattedDateFrom;
                var dateTo = formattedDateTo;

//get the table row, then get the table cell,
                var TableRow = document.getElementById("applicant_data");
                var TableCells = TableRow.getElementsByTagName("td");

                // get the table cell[6] which is the date
                var check = TableCells[6].innerText;
                //convert it to a string (i think it is anyways?)
                var dateCheck = check.toString();

         // remove the slashes in the strings
                var d1 = dateFrom.split("/");
                var d2 = dateTo.split("/");
                var c = dateCheck.split("/");

        //log for testing
                console.log(d1);
                console.log(d2);
                console.log(c);

        //convert the strings into dates and set them as a new variable
                var from = new Date(d1[2], d1[1]-1, d1[0]);
                var to = new Date(d2[2], d2[1]-1, d2[0]);
                var check1 = new Date(c[2], c[1]-1, c[0]);

         //log them out again
                console.log(from);
                console.log(to);
                console.log(check1);

The issue is with my output, look at this:

["03", "04", "2014"]
["03", "05", "2014"]
["08", "19", "2013"]
Thu Apr 03 2014 00:00:00 GMT-0400 (Eastern Daylight Time)
Sat May 03 2014 00:00:00 GMT-0400 (Eastern Daylight Time)

//this date below should be August 19 2013......
Tue Jul 08 2014 00:00:00 GMT-0400 (Eastern Daylight Time)

How is this happening?!???!? The third date is literally changing.

3
  • Use YYYY-MM-DD if you can help it; it’s unambiguous. Commented Mar 25, 2014 at 2:42
  • When all else fails, read the spec. :-) The format for creating a Date object is new Date (year, month, date) where months are zero indexed (i.e. 0 == January, 11 == December). The OP is using year, day, month. Use new Date(s[2], --s[0], s[1]). Commented Mar 25, 2014 at 3:10
  • Oh, and none of the converted dates are correct. e.g. 3/4/2014 in US format is 4 March, not 3 April and 3/5/2014 is 5 March, not 3 May. Commented Mar 25, 2014 at 3:12

1 Answer 1

3

19 is converted to a month instead of 8 and by the looks of it, it does a modulo operation. 19%12 = 7 a subtraction: 19 - 12 = 7 hence July. You may want to rearrange your date, otherwise it's the same case for the first 2. In other words, swap the month and day in your new Date call.

More details+(correction on earlier mistake):

var check1 = new Date(c[2], c[1]-1, c[0]);

In the code above, what you need to do is swap the last two so you have:

var check1 = new Date(c[2], c[0]-1, c[1]);

The function is expecting you to give the year, then month and day.

Sign up to request clarification or add additional context in comments.

2 Comments

I understand what you're saying but I'm not sure how to fix it. Could you help me out? I tried changing the values where it creates the date and it changed the year to 2086 but I'm not sure what the proper way to do it is.
FYI not modulo, it's subtraction. Think of it as number of months rolling over the number of months in a year. 24 would end up as December of the following year (24 - 12 = 12 month 12, not 0).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.