日期字符串的比较日期先后的函数封装(vue2)
js
compareDate(time1, time2) {
function compareTime(startdate, enddate) {
var date1 = startdate;
var date2 = enddate;
if (Date.parse(date1) < Date.parse(date2)) {
return '<'
} else if (Date.parse(date1) > Date.parse(date2)) {
return '>'
} else {
return '='
}
}
return compareTime(time1, time2)
}compareDate(time1, time2) {
function compareTime(startdate, enddate) {
var date1 = startdate;
var date2 = enddate;
if (Date.parse(date1) < Date.parse(date2)) {
return '<'
} else if (Date.parse(date1) > Date.parse(date2)) {
return '>'
} else {
return '='
}
}
return compareTime(time1, time2)
}不适用于不同格式直接的比较
js
// yyyy-M-d VS yyyy-M-d Good!
console.log(
this.compareDate('2022-1-1', '2023-1-1'),
this.compareDate('2023-1-1', '2023-1-1'),
this.compareDate('2023-1-1', '2022-1-1')
)
// yyyy-MM-dd VS yyyy-M-d Bad!不能进行不同格式的比较
console.log(
this.compareDate('2022-01-01', '2023-1-1'),
this.compareDate('2023-01-01', '2023-1-1'),
this.compareDate('2023-01-01', '2022-1-1')
)
// yyyy-MM-dd VS yyyy-MM-dd Good!
console.log(
this.compareDate('2022-01-01', '2023-01-01'),
this.compareDate('2023-01-01', '2023-01-01'),
this.compareDate('2023-01-01', '2022-01-01')
)// yyyy-M-d VS yyyy-M-d Good!
console.log(
this.compareDate('2022-1-1', '2023-1-1'),
this.compareDate('2023-1-1', '2023-1-1'),
this.compareDate('2023-1-1', '2022-1-1')
)
// yyyy-MM-dd VS yyyy-M-d Bad!不能进行不同格式的比较
console.log(
this.compareDate('2022-01-01', '2023-1-1'),
this.compareDate('2023-01-01', '2023-1-1'),
this.compareDate('2023-01-01', '2022-1-1')
)
// yyyy-MM-dd VS yyyy-MM-dd Good!
console.log(
this.compareDate('2022-01-01', '2023-01-01'),
this.compareDate('2023-01-01', '2023-01-01'),
this.compareDate('2023-01-01', '2022-01-01')
)获得今日日期 格式:yyyy-M-d
js
let pastResult = new Date().getTime()
var pastDate = new Date(pastResult),
pastYear = pastDate.getFullYear(),
pastMonth = pastDate.getMonth() + 1,
pastDay = pastDate.getDate();
let nowDate = pastYear + '-' + pastMonth + '-' + pastDay
console.log(nowDate)let pastResult = new Date().getTime()
var pastDate = new Date(pastResult),
pastYear = pastDate.getFullYear(),
pastMonth = pastDate.getMonth() + 1,
pastDay = pastDate.getDate();
let nowDate = pastYear + '-' + pastMonth + '-' + pastDay
console.log(nowDate)获得今日日期 获得格式:yyyy-MM-dd
js
let date = new Date(),
year = date.getFullYear(), //获取完整的年份(4位)
month = date.getMonth() + 1, //获取当前月份(0-11,0代表1月)
strDate = date.getDate() // 获取当前日(1-31)
if (month < 10) month = `0${month}` // 如果月份是个位数,在前面补0
if (strDate < 10) strDate = `0${strDate}` // 如果日是个位数,在前面补0
console.log(`${year}-${month}-${strDate}`)let date = new Date(),
year = date.getFullYear(), //获取完整的年份(4位)
month = date.getMonth() + 1, //获取当前月份(0-11,0代表1月)
strDate = date.getDate() // 获取当前日(1-31)
if (month < 10) month = `0${month}` // 如果月份是个位数,在前面补0
if (strDate < 10) strDate = `0${strDate}` // 如果日是个位数,在前面补0
console.log(`${year}-${month}-${strDate}`)给日期字符串的月份增加一个数值
html
<script>
export default {
methods: {
// 增加指定月份方法
addMonth(yyyy_mm_dd, addMonthNum) { // 函数(开始日期,要增加的日期)
// 先将字符串格式的时间类型转化为Date类型
if (yyyy_mm_dd.length > 10) {
var str = yyyy_mm_dd
} else {
var str = yyyy_mm_dd + ' 00:00:00'; //字符串格式的时间类型
}
var str1 = str.replace(/-/g, '/'); //'2018/01/01 00:00:00'
var date = new Date(Date.parse(str1)); //date格式的时间类型
//计算当前月最大天数
Date.daysInMonth = function (year, month) {
if (month == 1) {
if (year % 4 == 0 && year % 100 != 0)
return 29;
else
return 28;
} else if ((month <= 6 && month % 2 == 0) || (month > 6 && month % 2 == 1))
return 31;
else
return 30;
};
Date.prototype.addMonth = function (addMonth) {
var y = this.getFullYear();
var m = this.getMonth();
var nextY = y;
var nextM = m;
//如果当前月+增加的月>11 这里之所以用11是因为 js的月份从0开始
if ((m + addMonth) > 11) {
nextY = y + 1;
nextM = parseInt(m + addMonth) - 12;
} else {
nextM = this.getMonth() + addMonth
}
var daysInNextMonth = Date.daysInMonth(nextY, nextM);
var day = this.getDate();
if (day > daysInNextMonth) {
day = daysInNextMonth;
}
return new Date(nextY, nextM, day);
};
// 再将Date类型的时间增加指定月份
var nowDate = date.addMonth(addMonthNum); //date格式的时间类型
Date.prototype.format = function (format) {
var date = {
"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()
};
if (/(y+)/i.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
}
for (var k in date) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1
? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
}
}
return format;
};
// 最后将Date类型的时间在转化为字符串类型
// var nowStr = nowDate.format('yyyy-MM-dd hh:mm:ss'); //指定字符串格式的时间类型
var nowStr = nowDate.format('yyyy-MM-dd')
return nowStr
},
},
}
</script><script>
export default {
methods: {
// 增加指定月份方法
addMonth(yyyy_mm_dd, addMonthNum) { // 函数(开始日期,要增加的日期)
// 先将字符串格式的时间类型转化为Date类型
if (yyyy_mm_dd.length > 10) {
var str = yyyy_mm_dd
} else {
var str = yyyy_mm_dd + ' 00:00:00'; //字符串格式的时间类型
}
var str1 = str.replace(/-/g, '/'); //'2018/01/01 00:00:00'
var date = new Date(Date.parse(str1)); //date格式的时间类型
//计算当前月最大天数
Date.daysInMonth = function (year, month) {
if (month == 1) {
if (year % 4 == 0 && year % 100 != 0)
return 29;
else
return 28;
} else if ((month <= 6 && month % 2 == 0) || (month > 6 && month % 2 == 1))
return 31;
else
return 30;
};
Date.prototype.addMonth = function (addMonth) {
var y = this.getFullYear();
var m = this.getMonth();
var nextY = y;
var nextM = m;
//如果当前月+增加的月>11 这里之所以用11是因为 js的月份从0开始
if ((m + addMonth) > 11) {
nextY = y + 1;
nextM = parseInt(m + addMonth) - 12;
} else {
nextM = this.getMonth() + addMonth
}
var daysInNextMonth = Date.daysInMonth(nextY, nextM);
var day = this.getDate();
if (day > daysInNextMonth) {
day = daysInNextMonth;
}
return new Date(nextY, nextM, day);
};
// 再将Date类型的时间增加指定月份
var nowDate = date.addMonth(addMonthNum); //date格式的时间类型
Date.prototype.format = function (format) {
var date = {
"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()
};
if (/(y+)/i.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
}
for (var k in date) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1
? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
}
}
return format;
};
// 最后将Date类型的时间在转化为字符串类型
// var nowStr = nowDate.format('yyyy-MM-dd hh:mm:ss'); //指定字符串格式的时间类型
var nowStr = nowDate.format('yyyy-MM-dd')
return nowStr
},
},
}
</script>判断一个日期是否是工作日
判断给定的日期是否是工作日
js
const isWeekday = (date) => date.getDay() % 6 !== 0;
console.log(isWeekday(new Date(2021, 0, 11)));
// Result: true (周一)
console.log(isWeekday(new Date(2021, 0, 10)));
// Result: false (周日)const isWeekday = (date) => date.getDay() % 6 !== 0;
console.log(isWeekday(new Date(2021, 0, 11)));
// Result: true (周一)
console.log(isWeekday(new Date(2021, 0, 10)));
// Result: false (周日)检查日期是否有效
js
const isDateValid = (...val) => !Number.isNaN(new Date(...val).valueOf());
isDateValid("December 17, 1995 03:24:00");
// Result: trueconst isDateValid = (...val) => !Number.isNaN(new Date(...val).valueOf());
isDateValid("December 17, 1995 03:24:00");
// Result: true计算两天之间相差的天数
js
const dayDif = (date1, date2) => Math.ceil(Math.abs(date1.getTime() - date2.getTime()) / 86400000)
dayDif(new Date("2020-10-21"), new Date("2021-10-22"))
// Result: 366const dayDif = (date1, date2) => Math.ceil(Math.abs(date1.getTime() - date2.getTime()) / 86400000)
dayDif(new Date("2020-10-21"), new Date("2021-10-22"))
// Result: 366将天数转换为年、月、日
- 方法1
js
//Method 2
const calculateTimimg = d => {
let months = 0, years = 0, days = 0;
while(d){
if(d >= 365){
years++;
d -= 365;
}else if(d >= 30){
months++;
d -= 30;
}else{
days++;
d--;
}
};
return {
years, months, days
};
};//Method 2
const calculateTimimg = d => {
let months = 0, years = 0, days = 0;
while(d){
if(d >= 365){
years++;
d -= 365;
}else if(d >= 30){
months++;
d -= 30;
}else{
days++;
d--;
}
};
return {
years, months, days
};
};- 方法2
js
function getFormatedStringFromDays(numberOfDays) { // 将天数转换为X年X月X日
var years = Math.floor(numberOfDays / 365);
var months = Math.floor(numberOfDays % 365 / 30);
var days = Math.floor(numberOfDays % 365 % 30);
return [years, months, days].join(':');
}function getFormatedStringFromDays(numberOfDays) { // 将天数转换为X年X月X日
var years = Math.floor(numberOfDays / 365);
var months = Math.floor(numberOfDays % 365 / 30);
var days = Math.floor(numberOfDays % 365 % 30);
return [years, months, days].join(':');
}计算两个日期之间的天数
js
/**
* 计算两个日期之间的天数
* date1 开始日期 yyyy-MM-dd
* date2 结束日期 yyyy-MM-dd
* 如果日期相同 返回一天 开始日期大于结束日期,返回0
*/
function getDaysBetween(date1, date2) { // 计算两个日期之间的天数
var startDate = Date.parse(date1);
var endDate = Date.parse(date2);
if (startDate > endDate) {
return 0;
}
if (startDate == endDate) {
return 1;
}
var days = (endDate - startDate) / (1 * 24 * 60 * 60 * 1000);
return days;
}/**
* 计算两个日期之间的天数
* date1 开始日期 yyyy-MM-dd
* date2 结束日期 yyyy-MM-dd
* 如果日期相同 返回一天 开始日期大于结束日期,返回0
*/
function getDaysBetween(date1, date2) { // 计算两个日期之间的天数
var startDate = Date.parse(date1);
var endDate = Date.parse(date2);
if (startDate > endDate) {
return 0;
}
if (startDate == endDate) {
return 1;
}
var days = (endDate - startDate) / (1 * 24 * 60 * 60 * 1000);
return days;
}根据对象数组的日期属性进行排序
js
let list = [
{
name:'',
time:'2023-01-02'
},
{
name:'',
time:'2023-01-03'
},
]
for (let i = 0; i < this.list.length; i++) { // 对观影记录根据日期进行排序
for (let x = 0; x < this.list[i].total.length; x++) {
this.list[i].total.sort((a, b) => a.time.localeCompare(b.time) || a.time.localeCompare(b.time));
}
}let list = [
{
name:'',
time:'2023-01-02'
},
{
name:'',
time:'2023-01-03'
},
]
for (let i = 0; i < this.list.length; i++) { // 对观影记录根据日期进行排序
for (let x = 0; x < this.list[i].total.length; x++) {
this.list[i].total.sort((a, b) => a.time.localeCompare(b.time) || a.time.localeCompare(b.time));
}
}
liang14658fox