目录
1. 时间格式化 -- 转换为:yyyy-MM-dd hh:mm:ss格式
1. 时间格式化 -- 转换为:yyyy-MM-dd hh:mm:ss格式
// 说明:JS时间Date格式化参数 // 参数:格式化字符串如:'yyyy-MM-dd hh:mm:ss' // 结果:如2016-06-01 10:09:00
Date.prototype.format = function(fmt) {
let o = {
"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() // 毫秒
};
// 根据y的长度来截取年
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for (let k in o) {
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : ((
"00" + o[k]).substr(("" + o[k]).length)));
}
return fmt;
}
// 用法:
let time1 = new Date().format("yyyy-MM-dd");
let time2 = new Date(1469281964000).format("yyyy-MM-dd hh:mm:ss");
2. 某一天所在星期范围
// 参数:'2022-03-04' || 时间对象 // 结果:'2022-03-04 至2022-03-10'
function getWeekRange(date) {
if (!date) return
let now = new Date(date);
let nowDayOfWeek = now.getDay(); // 星期日—>六(0->6)
let nowDay = now.getDate();
let nowMonth = now.getMonth();
let nowYear = now.getYear();
nowYear += (nowYear < 2000) ? 1900 : 0;
let weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek + 1); // 这周的周五
let weekEndDate = new Date(nowYear, nowMonth, nowDay + (7 - nowDayOfWeek)); // 7- 这周的周日 && 用5- 得到这的周五
return weekStartDate.format('yyyy-MM-dd') + " 至 " + weekEndDate.format('yyyy-MM-dd');
}
// 用法:
getWeekRange('2022-03-05') // ‘2022-03-04 至 2022-03-10’
3. 获取当月第一天
//获取当月第一天
function getFirstDayOfMonth () {
let date = new Date();
date.setDate(1);
return date.format('yyyy-MM-dd');
}
4. 获取当月最后一天
function getFirstDayOfMonth () {
let date = new Date();
let month = date.getMonth();
date.setMonth(month+1);
date.setDate(0);
return date.format('yyyy-MM-dd');
}
5. 获取这个季度的第一天
function getFirstDayOfSeason() {
let date = new Date();
let month = date.getMonth();
if (month < 3) {
date.setMonth(0);
} else if (2 < month && month < 6) {
date.setMonth(3);
} else if (5 < month && month < 9) {
date.setMonth(6);
} else if (8 < month && month < 11) {
date.setMonth(9);
}
date.setDate(1);
return date.format('yyyy-MM-dd');;
}
6. 获取当年的第一天
function getFirstDayOfYear() {
let date = new Date();
date.setDate(1);
date.setMonth(0);
return date.format('yyyy-MM-dd');;
}
7. 获取当前日期为一年中的第几天
Math.ceil(( new Date() - new Date(new Date().getFullYear().toString()))/(24*60*60*1000))+1;
8. 获取今天还剩多少天
function restDayOfYear() {
let fullyear = new Date().getFullYear();
let nextyear = fullyear + 1;
let lastday = new Date(new Date(nextyear, 0, 1) - 1); //本年的最后一毫秒:
let now = new Date();
let diff = lastday - now; //毫秒数
return Math.ceil(diff / (1000 * 60 * 60 * 24));
}
9. 获取今天为周几
getWeekZh(date) {
let weekArrayList = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
let index = new Date(`${date}`).getDay()
let week = weekArrayList[index]
return week
}
10.当前直接转换为当前,昨天,以往时间 (针对于聊天室)
dateTime(e) {
let old = new Date(e);
let now = new Date();
//获取old具体时间
let d = old.getTime();
let h = old.getHours();
let m = old.getMinutes();
let Y = old.getFullYear();
let M = old.getMonth() + 1;
let D = old.getDate();
//获取now具体时间
let nd = now.getTime();
let nh = now.getHours();
let n = now.getMinutes();
let nY = now.getFullYear();
let nM = now.getMonth() + 1;
let nD = now.getDate();
//当天的时间
if (D === nD && M === nM && Y === nY) {
if (h < 10) {
h = '0' + h;
}
if (m < 10) {
m = '0' + m;
}
return h + ':' + m;
}
//昨天时间
if (D + 1 === nD && M === nM && Y === nY) {
if (h < 10) {
h = '0' + h;
}
if (m < 10) {
m = '0' + m;
}
return '昨天 ' + h + ':' + m;
} else if (Y == nY) {
//今年
if (h < 10) {
h = '0' + h;
}
if (m < 10) {
m = '0' + m;
}
return M + '月' + D + '日 ' + h + ':' + m
} else {
//大于今年
if (h < 10) {
h = '0' + h;
}
if (m < 10) {
m = '0' + m;
}
return Y + '年' + M + '月' + D + '日 ' + h + ':' + m
}
},