정말 이상한 문제가 있습니다.Date()로 변환하면 Javascript 문자열이 변경됩니다.
두 개의 텍스트 상자에서 데이터를 가져 오는 두 개의 자바 스크립트 문자열 변수가 있습니다.
세 번째 문자열이 표 셀에서 꺼내집니다. 그러나 Javascript Date()로 변환하면 날짜가 변경됩니다. 왜 이런 일이 일어날 지 모르겠다.
나는 무슨 일이 일어나는지 설명하는 데 도움이되는 코드로 의견을 말합니다.
["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)
어떻게 이런 일이 ????!
//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);
문제는 이것 좀 봐, 내 출력입니까? 세 번째 데이트는 말 그대로 변화합니다.
도움이 될만한 경우에는 'YYYY-MM-DD'를 사용하십시오. 모호하지 않습니다. – Ryan
다른 모든 것이 실패하면 [read the spec] (http://ecma-international.org/ecma-262/5.1/#sec-15.9.3.1). :-) Date 객체를 생성하기위한 형식은 월이 0으로 색인 된 (즉, 0 == 1 월, 11 == 12 월) 'new Date (year, month, date)'입니다. OP는 '년, 일, 월'을 사용하고 있습니다. '새로운 날짜 (s [2], --s [0], s [1])'를 사용하십시오. – RobG
아, 변환 된 날짜 중 어느 것도 맞지 않습니다. 예 : 2014 년 3 월 4 일 미국 형식은 3 월 4 일이며 4 월 3 일과 3/5/2014는 3 월 5 일이며 5 월 3 일이 아닙니다. – RobG