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( ); //獲取日期與時間