js-Date扩展format()函数--处理时间字符串格式
const formatNumber = n => {
n = n.toString()
return n[1] ? n : `0${n}`
}
处理月份和天的日期字符串,就是个位数前面加0的截取处理,或者两位数的截取处理
('0' + dateNum).substr(-2, 2)
// js Date扩展Format()函数 处理时间字符串格式
/**
* 实参formatStr格式为 'yyyy-MM-dd hh:mm:ss'
* 实参formatStr里面的月份MM必须大写
*
*/
Date.prototype.format = function (formatStr) {
var str = formatStr;
var Week = ['日', '一', '二', '三', '四', '五', '六'];
str = str.replace(/yyyy|YYYY/, this.getFullYear());
str = str.replace(/yy|YY/, (this.getYear() % 100) > 9 ? (this.getYear() % 100).toString() : '0' + (this.getYear() % 100));
str = str.replace(/MM/, this.getMonth() > 9 ? (this.getMonth() + 1).toString() : '0' + (this.getMonth() + 1));
str = str.replace(/M/g, this.getMonth());
str = str.replace(/w|W/g, Week[this.getDay()]);
str = str.replace(/dd|DD/, this.getDate() > 9 ? this.getDate().toString() : '0' + this.getDate());
str = str.replace(/d|D/g, this.getDate());
str = str.replace(/hh|HH/, this.getHours() > 9 ? this.getHours().toString() : '0' + this.getHours());
str = str.replace(/h|H/g, this.getHours());
str = str.replace(/mm/, this.getMinutes() > 9 ? this.getMinutes().toString() : '0' + this.getMinutes());
str = str.replace(/m/g, this.getMinutes());
str = str.replace(/ss|SS/, this.getSeconds() > 9 ? this.getSeconds().toString() : '0' + this.getSeconds());
str = str.replace(/s|S/g, this.getSeconds());
return str;
};
// var aa = new Date().format('yyyy-MM-dd hh:mm:ss')
var aa = new Date().format('yyyy-MM-dd')
console.log(aa)
// 获取一个月前的时间
const getLastMonthYestdy = (date) => {
// var date = new Date(); // 1 2 3 4 5 6 7 8 9 10 11 12月
var daysInMonth = new Array([0], [31], [28], [31], [30], [31], [30], [31], [31], [30], [31], [30], [31]);
var strYear = date.getFullYear();
var strDay = date.getDate();
var strMonth = date.getMonth() + 1;
if (strYear % 4 == 0 && strYear % 100 != 0) { //一、解决闰年平年的二月份天数 //平年28天、闰年29天//能被4整除且不能被100整除的为闰年
daysInMonth[2] = 29;
}
if (strMonth - 1 == 0) //二、解决跨年问题
{
strYear -= 1;
strMonth = 12;
} else {
strMonth -= 1;
}
// strDay = daysInMonth[strMonth] >= strDay ? strDay : daysInMonth[strMonth];
strDay = Math.min(strDay, daysInMonth[strMonth]); //三、前一个月日期不一定和今天同一号,例如3.31的前一个月日期是2.28;9.30前一个月日期是8.30
if (strMonth < 10) //给个位数的月、日补零
{
strMonth = "0" + strMonth;
}
if (strDay < 10) {
strDay = "0" + strDay;
}
let datastr = strYear + "/" + strMonth + "/" + strDay;
return datastr;
}
const formatTime = date => {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()
return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}
const formatTimeHanzhi = date => {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()
return year + '年' + month + '月' + day + '日 ' + hour + '时' + minute + '分' + second + '秒';
// return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}
标签:const,format,--,js,strMonth,str,date,replace,strDay
From: https://www.cnblogs.com/lifan-fineDay/p/16873684.html