总结js 对时间的处理常用方法
//获取时间戳
function getTimeTamp(date) {
var dateTime = new Date(Date.parse(date));
dateTime = dateTime.getTime(); // 获取时间戳
return dateTime;
}
//获取当前时刻
function getNowDateTamp() {
var dateTime = new Date();
dateTime = dateTime.getTime(); // 获取时间戳
return dateTime;
}
// 日期按指定格式输出(例如: 2018-09-03 17:09:45)
function getDateFormat(date, flag) {
var dateTime = date;
var d = new Date(dateTime);
var year = d.getFullYear();
var month = d.getMonth() + 1;
var day = d.getDate();
var hour = d.getHours();
var minute = d.getMinutes();
var second = d.getSeconds();
month = month < 10 ? "0" + month : month;
day = day < 10 ? "0" + day : day;
hour = hour < 10 ? "0" + hour : hour;
minute = minute < 10 ? "0" + minute : minute;
second = second < 10 ? "0" + second : second;
if (flag) {
return year + "-" + month + "-" + day;
}
// 结果返回
return (
year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second
);
}