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.
YYYY-MM-DDif you can help it; it’s unambiguous.new Date (year, month, date)where months are zero indexed (i.e. 0 == January, 11 == December). The OP is usingyear, day, month. Usenew Date(s[2], --s[0], s[1]).