2014-03-25 2 views
0

정말 이상한 문제가 있습니다.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); 

문제는 이것 좀 봐, 내 출력입니까? 세 번째 데이트는 말 그대로 변화합니다.

+0

도움이 될만한 경우에는 'YYYY-MM-DD'를 사용하십시오. 모호하지 않습니다. – Ryan

+0

다른 모든 것이 실패하면 [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

+0

아, 변환 된 날짜 중 어느 것도 맞지 않습니다. 예 : 2014 년 3 월 4 일 미국 형식은 3 월 4 일이며 4 월 3 일과 3/5/2014는 3 월 5 일이며 5 월 3 일이 아닙니다. – RobG

답변

3

19는 8 대신 월로 변환되며, 그 모양에 따라 모듈로 연산을 수행합니다 ( ). 19 % 12 = 7 빼기 : 19 - 12 = 7 따라서 7 월. 날짜를 다시 정렬해야 할 수도 있습니다. 그렇지 않으면 첫 번째 2의 경우와 동일합니다. 즉, new Date 호출에서 월과 일을 바꿉니다.

자세한 내용 + (이전의 실수에 대한 보정) : 위의 코드에서

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

, 당신이해야 할 일이 스왑 마지막 두 그래서 당신은 :

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

이 함수는 연도, 월, 일을 제공 할 것으로 기대합니다.

+0

나는 당신이 말하는 것을 이해하지만 그것을 고치는 방법을 모르겠습니다. 너 좀 도와 줄 수있어? 나는 날짜를 생성하고 2086 년으로 바꾼 값을 변경하려고 시도했지만 적절한 방법이 무엇인지 확실하지 않습니다. –

+0

@ NicolasWard 업데이트 된 답변으로 질문이 명확하게 정리되어 있습니까? – Julio

+0

참고로 모듈러스가 아니라 빼기입니다. 연간 수를 넘는 달 수로 생각하십시오. 24는 다음 해 12 월로 끝날 것입니다 (24 - 12 = 12 월 12 일, 0이 아님). –

관련 문제