2017-11-23 10 views
0

timestamp를 함수 아래로 전달하고 올바르게 날짜 문자열을 반환하지만 아래 행을 실행하면 잘못된 날짜가 잘못 표시됩니다.새로운 날짜()를 사용하는 잘못된 날짜 문제

 var postDate = new Date(this.ConvertServerDateToLocal(timestamp)); 

//postDate returns invalid date object. 


     ConvertServerDateToLocal: function(dateInput){ 
      // EST - UTC offset: 4 hours 

      var offset = 4.0, 
      /* 
      - calculate the difference between the server EST date and UTC 
      - the value returned by the getTime method is the number of milliseconds since 1 January 1970 00:00:00 UTC. 
      - the time-zone offset is the difference, in minutes, between UTC and local time 
      - 60000 milliseconds = 60 seconds = 1 minute 
      */ 
       serverDate = new Date(dateInput), 
       utc = serverDate.getTime() - (serverDate.getTimezoneOffset() * 60000), 
      /* 
      - apply the offset between UTC and EST (4 hours) 
      - 3600000 milliseconds = 3600 seconds = 60 minutes = 1 hour 
      */ 
      clientDate = new Date(utc + (3600000 * offset)); 
      return clientDate.toLocaleString(); 
     } 

다음은 ConvertServerDateToLocal() 함수에 전달하는 타임 스탬프의 예입니다.

타임 스탬프 = "2017년 11월 22일 23시 5분 58초"// // = "2017년 11월 9일 18시 30분 19초"출력 후

타임 스탬프를 유효하지 않은 날짜를 던지는 제대로

답변

0
function ConvertServerDateToLocal(dateInput) { 
    // EST - UTC offset: 4 hours 
    dateInput = "Nov 09, 2017 18:30:19"; 
    var offset = 4.0, 
    /* 
    - calculate the difference between the server EST date and UTC 
    - the value returned by the getTime method is the number of milliseconds since 1 January 1970 00:00:00 UTC. 
    - the time-zone offset is the difference, in minutes, between UTC and local time 
    - 60000 milliseconds = 60 seconds = 1 minute 
    */ 
    serverDate = new Date(dateInput.toString()); 

    utc = serverDate.getTime() - (serverDate.getTimezoneOffset() * 60000); 

    /* 
    - apply the offset between UTC and EST (4 hours) 
    - 3600000 milliseconds = 3600 seconds = 60 minutes = 1 hour 
    */ 
    clientDate = new Date(utc + (3600000 * offset)); 

    //return clientDate.toLocaleString(); 
    alert(clientDate.toLocaleString()); 
} 
작업

입력 매개 변수를 string으로 변환하면 효과적입니다. 그러나 왜 한 날짜가 작동하고 다른 날짜가 작동하지 않았는지 잘 모르겠습니다. 답변을 찾은 경우 여기에 게시하십시오. 나는 또한 날짜를 많이 사용하며 항상 고통 스럽다.