用js完成毫秒格式数据的日期格式化任务

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

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

  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);

 

  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.         }  

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

  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.         };  

转载于:https://www.cnblogs.com/jiangyi666/p/5984752.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值