JS、Jquery实现---把后台传过来的时间毫秒数转换成想要的日期格式

来源:互联网 发布:缩小手机屏幕的软件 编辑:程序博客网 时间:2024/05/21 06:41

后台传过来的数据  creationTime  在后台是Date类型的


毫秒转换成  05-24  月 日格式的

[python] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //获得月日得到日期oTime  
  2.         function getMoth(str){  
  3.             var oDate = new Date(str),  
  4.             oMonth = oDate.getMonth()+1,  
  5.             oDay = oDate.getDate(),  
  6.             oTime = getzf(oMonth) +'-'+ getzf(oDay);//最后拼接时间  
  7.             return oTime;  
  8.         };  
  9.         console.log(getMoth(1465959000));//使用方法  


毫秒转换成 年月日+时分秒 格式的  1970-01-18 07:12:39   

补0操作: 比如2012-2-2  就会变成    2012-02-02

使用方法: getMyDate(data[i].creationTime);

[python] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //获得年月日      得到日期oTime  
  2.         function getMyDate(str){  
  3.             var oDate = new Date(str),  
  4.             oYear = oDate.getFullYear(),  
  5.             oMonth = oDate.getMonth()+1,  
  6.             oDay = oDate.getDate(),  
  7.             oHour = oDate.getHours(),  
  8.             oMin = oDate.getMinutes(),  
  9.             oSen = oDate.getSeconds(),  
  10.             oTime = oYear +'-'+ getzf(oMonth) +'-'+ getzf(oDay) +' '+ getzf(oHour) +':'+ getzf(oMin) +':'+getzf(oSen);//最后拼接时间  
  11.             return oTime;  
  12.         };  
  13.         //补0操作  
  14.         function getzf(num){  
  15.             if(parseInt(num) < 10){  
  16.                 num = '0'+num;  
  17.             }  
  18.             return num;  
  19.         }  



毫秒转换成  年月日 时分秒的格式

[python] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /*  
  2.         js由毫秒数得到年月日  
  3.         使用: (new Date(data[i].creationTime)).Format("yyyy-MM-dd hh:mm:ss.S")  
  4.         */  
  5.         Date.prototype.Format = function (fmt) { //author: meizz  
  6.             var o = {  
  7.                 "M+": this.getMonth() + 1, //月份  
  8.                 "d+": this.getDate(), //日  
  9.                 "h+": this.getHours(), //小时  
  10.                 "m+": this.getMinutes(), //分  
  11.                 "s+": this.getSeconds(), //秒  
  12.                 "q+": Math.floor((this.getMonth() + 3) / 3), //季度  
  13.                 "S": this.getMilliseconds() //毫秒  
  14.             };  
  15.             if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));  
  16.             for (var k in o)  
  17.                 if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));  
  18.             return fmt;  
  19.         };  
0 0
原创粉丝点击