时间格式化方法

来源:互联网 发布:铁木辛柯 知乎 编辑:程序博客网 时间:2024/06/05 08:39

1 new Date()转换成2015-12-11 12:22:22格式的时间方法

//获得年月日 1970-01-18 07:12:39function getMyDate(str){    var oDate = new Date(str),        oYear = oDate.getFullYear(),        oMonth = oDate.getMonth()+1,        oDay = oDate.getDate(),        oHour = oDate.getHours(),        oMin = oDate.getMinutes(),        oSen = oDate.getSeconds(),        oTime = oYear +'-'+ getzf(oMonth) +'-'+ getzf(oDay) +' '+ getzf(oHour) +':'+ getzf(oMin) +':'+getzf(oSen);//最后拼接时间    return oTime;};//补0操作function getzf(num){    if(parseInt(num) < 10){        num = '0'+num;    }    return num;}

2 . 毫秒值(时间戳)转换成自己想要的格式(扩展Date.prototype.toLocalString()方法)

得到后台从数据库中拿到的数据我们希望格式是2016年10月25日 17时37分30秒 或者 2016/10/25 17:37:30,然而我们前台得到的却是一段数字(时间戳,毫秒数)
1477386005,我们要将时间戳转化为我们想要的格式。

   var yestdayDate = $('#tradeStartDate').datebox('getValue');//2015-02-11    var newDate = new Date(yestdayDate);  // Thu Feb 24 2017 08:00:00 GMT+0800 (中国标准时间)    var yestMilc = newDate.getTime(); // 14121243356    var cha = yestMilc -24*60*60*1000;//昨天的毫秒值    var unixTimeStamp = new Date(cha); // Thu Feb 23 2017 08:00:00 GMT+0800 (中国标准时间)    console.log(unixTimeStamp);    var commontime = unixTimeStamp.toLocaleString();    console.log("时间未:"+commontime);//扩展toLocalString方法 格式 :2016年10月3日 2点3分3秒Date.prototype.toLocaleString = function() {          return this.getFullYear() + "年" + (this.getMonth() + 1) + "月" + this.getDate() + "日 " + this.getHours() + "点" + this.getMinutes() + "分" + this.getSeconds() + "秒";    };//或者 :格式:2015/10/12 12:12:22Date.prototype.toLocaleString = function() {          return this.getFullYear() + "/" + (this.getMonth() + 1) + "/" + this.getDate() + "/ " + this.getHours() + ":" + this.getMinutes() + ":" + this.getSeconds();    };
0 0
原创粉丝点击