日期相关操作记录
毫秒数转标准格式日期
function formatterTime(times){
let date = new Date(times);
var year = date.getFullYear(); // 年份
var month = date.getMonth() + 1; // 月份
var strDate = date.getDate(); // 一月中的某一日
var hours = date.getHours(), // 小时
minutes = date.getMinutes(), // 分钟
seconds = date.getSeconds(); // 秒数
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
if (hours >= 0 && hours <= 9) {
hours = "0" + hours;
}
if (minutes >= 0 && minutes <= 9) {
minutes = "0" + minutes;
}
if (seconds >= 0 && seconds <= 9) {
seconds = "0" + seconds;
}
let currentDate = year + "-" + month + "-" + strDate + " " +
hours + ":" + minutes + ":" + seconds;
return currentDate;
}
还有获取星期几的方法getDay()
let weekDays = ["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
let current = new Date();
weekDays[current.getDay()]; // "星期日"
对当前日期进行操作
let current= new Date();
// 获取当前日期时间 格式 yyyy/MM/dd
let currentDate = current.toLocaleDateString();
// 获取当前时间 格式 hh:mm:ss GMT+0800 (中国标注时间)
let currentDay = current.toTimeString();
// 格式处理一下 输出 yyyy-MM-dd hh:mm:ss
currentDate.replace(/\//g,"-")+" "+currentDay.split(" ")[0];