獲取後臺時間的處理
阿新 • • 發佈:2017-10-18
test mat r+ seconds sda get true div 多說
前段時間趕項目,突然遇到後臺獲取時間有八小時的一個差距,自己寫了幾種方法,也是怕自己忘記,所以分享出來
不多說,直接上代碼
//獲取當前的時間戳 function transdate(endTime){ var date=new Date(); date.setFullYear(endTime.substring(0,4)); date.setMonth(endTime.substring(5,7)-1); date.setDate(endTime.substring(8,10)); date.setHours(endTime.substring(11,13)); date.setMinutes(endTime.substring(14,16)); date.setSeconds(endTime.substring(17,19)); return Date.parse(date); } // var time=$("input").val(); var time="2017-08-13T02:11:25"; var sart="2017-08-13T04:12:40"; var Time= new Date(); var end=transdate(sart)-transdate(time); //轉化成時分秒00:00:00 function hours(msd) { var time = parseFloat(msd) / 1000; var hours=parseInt(time / 3600.0); var minute=parseInt((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60); var second=parseInt((parseFloat((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60) - parseInt((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60)) * 60); time = fixZero(hours,2) + ":" + fixZero(minute,2) + ":" + fixZero(second,2) ; return time; } console.log(Time); console.log(end); console.log(hours(end)); console.log(time); console.log(transdate(time)); console.log(Date.parse(Time)); //返回2017-01-01 01:01:00 function formatDate(now) { var now= new Date(now); var year=now.getFullYear(); var month=now.getMonth()+1; var date=now.getDate(); var hour=now.getHours(); var minute=now.getMinutes(); var second=now.getSeconds(); return year+"-"+fixZero(month,2)+"-"+fixZero(date,2)+" "+fixZero(hour,2)+":"+fixZero(minute,2)+":"+fixZero(second,2); } //時間不足的地方補0 function fixZero(num,length){ var str=""+num; var len=str.length; var s=""; for(var i=length;i-->len;){ s+="0"; } return s+str; } //得到時間2017-8-2 13:00:00 function getDate(time1) { var tt=new Date(parseInt(time1)*1000).toLocaleDateString().replace(/\//g, "-") + " " + new Date(parseInt(time1)*1000).toTimeString().substr(0, 8); return tt; } var time1=transdate(time); var d=new Date(parseInt(time1)); console.log(getDate(time1)); console.log(formatDate(time1)); console.log(formatDate(Date.parse(Time)));
獲取後臺時間的處理