码迷,mamicode.com
首页 > 其他好文 > 详细

获取后台时间的处理

时间:2017-10-18 12:37:26      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:date()   get   cti   nbsp   new   自己   分享   获取   sharp   

前段时间赶项目,突然遇到后台获取时间有八小时的一个差距,自己写了几种方法,也是怕自己忘记,所以分享出来

不多说,直接上代码

 

//获取当前的时间戳
    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)));

  

获取后台时间的处理

标签:date()   get   cti   nbsp   new   自己   分享   获取   sharp   

原文地址:http://www.cnblogs.com/share901124/p/7685969.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!