一、代码封装
const getDateFn ={
//1、 得到今天、昨天、明天日期
//dates为数字类型,0代表今日,-1代表昨日,1代表明日,返回yyyy-mm-dd格式字符串,dates不传默认代表今日。
getDate(dates) {
var dd = new Date();
var n = dates || 0;
dd.setDate(dd.getDate() + n);
var y = dd.getFullYear();
var m = dd.getMonth() + 1;
var d = dd.getDate();
m = m < 10 ? "0" + m: m;
d = d < 10 ? "0" + d: d;
var day = y + "-" + m + "-" + d;
return day;
},
//2、得到本周、上周、下周的起始、结束日期
//type为字符串类型,有两种选择,"s"代表开始,"e"代表结束,dates为数字类型,不传或0代表本周,-1代表上
getMonday(type, dates) {
var now = new Date();
var nowTime = now.getTime();
var day = now.getDay();
var longTime = 24 * 60 * 60 * 1000;
var n = longTime * 7 * (dates || 0);
if (type == "s") {
var dd = nowTime - (day - 1) * longTime + n;
};
if (type == "e") {
var dd = nowTime + (7 - day) * longTime + n;
};
dd = new Date(dd);
var y = dd.getFullYear();
var m = dd.getMonth() + 1;
var d = dd.getDate();
m = m < 10 ? "0" + m: m;
d = d < 10 ? "0" + d: d;
var day = y + "-" + m + "-" + d;
return day;
},
//3、得到本月、上月、下月的起始、结束日期
//type为字符串类型,有两种选择,"s"代表开始,"e"代表结束,months为数字类型,0代表本月,-1代表上月,1代表下月
getMonth(type, months) {
var d = new Date();
var year = d.getFullYear();
var month = d.getMonth() + 1;
if (Math.abs(months) > 12) {
months = months % 12;
};
if (months != 0) {
if (month + months > 12) {
year++;
month = (month + months) % 12;
} else if (month + months < 1) {
year--;
month = 12 + month + months;
} else {
month = month + months;
};
};
month = month < 10 ? "0" + month: month;
var date = d.getDate();
var firstday = year + "-" + month + "-" + "01";
var lastday = "";
if (month == "01" || month == "03" || month == "05" || month == "07" || month == "08" || month == "10" || month == "12") {
lastday = year + "-" + month + "-" + 31;
} else if (month == "02") {
if ((year % 4 == 0 && year % 100 != 0) || (year % 100 == 0 && year % 400 == 0)) {
lastday = year + "-" + month + "-" + 29;
} else {
lastday = year + "-" + month + "-" + 28;
};
} else {
lastday = year + "-" + month + "-" + 30;
};
var day = "";
if (type == "s") {
day = firstday;
} else {
day = lastday;
};
return day;
},
//4、得到今年、去年、明年的开始、结束日期
//type为字符串类型,有两种选择,"s"代表开始,"e"代表结束,dates为数字类型,不传或0代表今年,-1代表去年,1代表明年
getYear(type, dates) {
var dd = new Date();
var n = dates || 0;
var year = dd.getFullYear() + Number(n);
if (type == "s") {
var day = year + "-01-01";
};
if (type == "e") {
var day = year + "-12-31";
};
if (!type) {
var day = year + "-01-01/" + year + "-12-31";
};
return day;
},
}
export default getDateFn;
二、使用
<template>
<div></div>
</template>
<script>
import getDateFn from '@/utils/getDateFn'
export default {
created() {
console.log('今日日期',getDateFn.getDate());
console.log('本周开始日期',getDateFn.getMonday('s'));
console.log('本周结束日期',getDateFn.getMonday('e'));
console.log('上周开始日期',getDateFn.getMonday('s',-1))
console.log('上周结束日期',getDateFn.getMonday('e',-1))
console.log('本月开始日期',getDateFn.getMonth('s',0));
console.log('本月结束日期',getDateFn.getMonth('e',0));
}
}
</script>
文章来源地址https://www.toymoban.com/news/detail-509370.html
文章来源:https://www.toymoban.com/news/detail-509370.html
到了这里,关于js 获取本周、上周、本月、上月、本年和上年开始和结束日期的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!