zoukankan      html  css  js  c++  java
  • 用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.         };  
  • 相关阅读:
    pat甲级1013
    二分查找
    pat甲级1012
    win10 + Ubuntu16.04双系统安装
    win10 U盘重装
    win10蓝牙添加设备无法连接
    Android自定义控件总结
    11.粘性控件
    10.侧拉删除
    9.视差特效、回弹动画、overScrollBy
  • 原文地址:https://www.cnblogs.com/jiangyi666/p/5984752.html
Copyright © 2011-2022 走看看