1.转换为年月日java
new Date(data.createDate).toLocaleDateString()//将json中的时间戳转换为年月日
2.精确到秒json
function 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; }
3.精确到毫秒this
function 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(),//秒 oFf=oDate.getMilliseconds()//毫秒 oTime = oYear +'-'+ getzf(oMonth) +'-'+ getzf(oDay) +' '+ getzf(oHour) +':'+ getzf(oMin) +':'+getzf(oSen)+':'+getzf(oFf);//最后拼接时间 return oTime; }; //补0操做 function getzf(num){ if(parseInt(num) < 10){ num = '0'+num; } return num; }
4.重写tolocaleString方法prototype
Date.prototype.toLocaleString = function() { return this.getFullYear() + "年" + (this.getMonth() + 1) + "月" + this.getDate() + "日 " + this.getHours() + "点" + this.getMinutes() + "分" + this.getSeconds() + "秒"; }; new Date(此处放时间戳).toLocaleString();
5.时间格式转换各类方法code
var mydate = new Date(); mydate.getYear(); //获取当前年份(2位) mydate.getFullYear(); //获取完整的年份(4位,1970-????) mydate.getMonth(); //获取当前月份(0-11,0表明1月) mydate.getDate(); //获取当前日(1-31) mydate.getDay(); //获取当前星期X(0-6,0表明星期天) mydate.getTime(); //获取当前时间(从1970.1.1开始的毫秒数) mydate.getHours(); //获取当前小时数(0-23) mydate.getMinutes(); //获取当前分钟数(0-59) mydate.getSeconds(); //获取当前秒数(0-59) mydate.getMilliseconds(); //获取当前毫秒数(0-999) mydate.toLocaleDateString(); //获取当前日期 var mytime=mydate.toLocaleTimeString(); //获取当前时间 mydate.toLocaleString( ); //获取日期与时间