Home >Web Front-end >JS Tutorial >Example code for timestamp and time format exchange
// 时间戳转化为时间 Date.prototype.format = function(format) { var date = { "M+": this.getMonth() + 1, "d+": this.getDate(), "h+": this.getHours(), "m+": this.getMinutes(), "s+": this.getSeconds(), "q+": Math.floor((this.getMonth() + 3) / 3), "S+": this.getMilliseconds() }; if (/(y+)/i.test(format)) { format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length)); } for (var k in date) { if (new RegExp("(" + k + ")").test(format)) { format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? date[k] : ("00" + date[k]).substr(("" + date[k]).length)); } } return format; };
使用:
var effStartDate ="1494565993";//你的时间戳
var date= new Date(); date.setTime(effStartDate); effStartDate = date.format('yyyy-MM-dd h:m:s'); //转化后的时间
时间转为时间戳:
var stringTime = "2017-06-16 12:21:12";var timestamp2 = Date.parse(new Date(stringTime)); timestamp2 = timestamp2 / 1000; //如果前后端是按照毫秒为单位,那么不用除以1000 console.log(timestamp2);//转化后的时间戳
The above is the detailed content of Example code for timestamp and time format exchange. For more information, please follow other related articles on the PHP Chinese website!