javascript date conversion showing wrong month

风格不统一 提交于 2019-12-14 04:05:19

问题


In the following date conversion after converting back the long integer The date says october instead of september

 var date = 2013-09-23 18:31
 startdate = getTimeStamp(date); //1382533260000

Now

 t=1382533260000   
 rt = new Date(t)
 //Wed Oct 23 2013 18:31:00 GMT+0530 (India Standard Time)


 function getTimeStamp(strDate) { 
            var a1=strDate.split(" ");
            var d1=a1[0].split("-");
            var t1=a1[1].split(":");
            var dtObj = new Date(d1[0],d1[1],d1[2],t1[0],t1[1]);
            return dtObj.getTime();
 }

回答1:


Months are zero-based, so January is zero, February is one, etc..

So you need to use d1[1]-1 in your new Date() constructor.




回答2:


In JavaScript, month numbers are numbered 0-11.

If you're parsing from components like this into the Date constructor you'll have to subtract one from the number:

 function getTimeStamp(strDate) { 
            var a1=strDate.split(" ");
            var d1=a1[0].split("-");
            var t1=a1[1].split(":");
            var dtObj = new Date(d1[0],d1[1] - 1,d1[2],t1[0],t1[1]);
            return dtObj.getTime();
 }



回答3:


Javascript month parameter starts from 0 upto 11 so, passing 8 means september



来源:https://stackoverflow.com/questions/19244052/javascript-date-conversion-showing-wrong-month

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!