JS常用的时间处理方法
目录
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
-
}
-
},
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgheeaj
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
怎样阻止微信小程序自动打开
PHP中文网 06-13