1 new Date()转换成2015-12-11 12:22:22格式的时间方法
//获得年月日 1970-01-18 07:12:39
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;
}
2 . 毫秒值(时间戳)转换成自己想要的格式(扩展Date.prototype.toLocalString()方法)
得到后台从数据库中拿到的数据我们希望格式是2016年10月25日 17时37分30秒 或者 2016/10/25 17:37:30,然而我们前台得到的却是一段数字(时间戳,毫秒数)
1477386005,我们要将时间戳转化为我们想要的格式。
var yestdayDate = $('#tradeStartDate').datebox('getValue');//2015-02-11
var newDate = new Date(yestdayDate); // Thu Feb 24 2017 08:00:00 GMT+0800 (中国标准时间)
var yestMilc = newDate.getTime(); // 14121243356
var cha = yestMilc -24*60*60*1000;//昨天的毫秒值
var unixTimeStamp = new Date(cha); // Thu Feb 23 2017 08:00:00 GMT+0800 (中国标准时间)
console.log(unixTimeStamp);
var commontime = unixTimeStamp.toLocaleString();
console.log("时间未:"+commontime);
//扩展toLocalString方法 格式 :2016年10月3日 2点3分3秒
Date.prototype.toLocaleString = function() {
return this.getFullYear() + "年" + (this.getMonth() + 1) + "月" + this.getDate() + "日 " + this.getHours() + "点" + this.getMinutes() + "分" + this.getSeconds() + "秒";
};
//或者 :格式:2015/10/12 12:12:22
Date.prototype.toLocaleString = function() {
return this.getFullYear() + "/" + (this.getMonth() + 1) + "/" + this.getDate() + "/ " + this.getHours() + ":" + this.getMinutes() + ":" + this.getSeconds();
};