js实现今天、昨天、本周、上周、本月、上月的开始结束日期
日期:2020-04-19
来源:程序思维浏览:3705次
这几天做点餐系统的报表统计,需要快捷查看今天,昨天,本周,上周,本月,上月等功能,那么就需要获取日期,转成对应的YYYY-mm-dd格式,下面看看是怎么做的吧:
今天:
//时间戳转日期
function formatMsgTime (timespan) {
var dateTime = new Date(timespan);
var year = dateTime.getFullYear();
var month = dateTime.getMonth() + 1;
var day = dateTime.getDate();
var timeSpanStr;
timeSpanStr = year + '-' + (month < 10 ? ('0' + month) : month) + '-' + (day<10?('0'+day):day);
return timeSpanStr;
}
//今天的时间戳
var noTime=Date.parse(new Date())-1000
formatMsgTime(noTime)//转成日期格式比如:2020-4-20
昨天:
//昨天的时间戳
var time=Date.parse(new Date())-1000*60*60*24*1
formatMsgTime(time)//转成日期格式比如:2020-4-19
本月、上月:
function getMonth(){
var now = new Date(); //当前日期
var nowMonth = now.getMonth(); //当前月
var nowYear = now.getFullYear(); //当前年
var lastMonthDate = new Date(); //上月日期
lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
var lastMonth = lastMonthDate.getMonth();
//格式化日期:yyyy-MM-dd
function formatDate(date) {
var myyear = date.getFullYear();
var mymonth = date.getMonth() + 1;
var myweekday = date.getDate();
if (mymonth < 10) {
mymonth = "0" + mymonth;
}
if (myweekday < 10) {
myweekday = "0" + myweekday;
}
return (myyear + "-" + mymonth + "-" + myweekday);
}
//获得某月的天数
function getMonthDays(myMonth) {
var monthStartDate = new Date(nowYear, myMonth, 1);
var monthEndDate = new Date(nowYear, myMonth + 1, 1);
var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
return days;
}
//获得本月的开始日期
function getMonthStartDate() {
var monthStartDate = new Date(nowYear, nowMonth, 1);
return formatDate(monthStartDate);
}
//获得本月的结束日期
function getMonthEndDate() {
var monthStartDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
return formatDate(monthStartDate);
}
//获得上月开始时间
function getLastMonthStartDate() {
var lastMonthStartDate = new Date(nowYear, lastMonth, 1);
return formatDate(lastMonthStartDate);
}
//获得上月结束时间
function getLastMonthEndDate() {
var lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth));
return formatDate(lastMonthEndDate);
}
return {
getMonthStartDate:getMonthStartDate,
getMonthEndDate:getMonthEndDate,
getLastMonthStartDate:getLastMonthStartDate,
getLastMonthEndDate:getLastMonthEndDate
}
}
//本月开始
console.log(getMonth.getMonthStartDate());
//本月结束
console.log(getMonth.getMonthEndDate());
//上月开始
console.log(getMonth.getLastMonthStartDate());
//上月结束
console.log(getMonth.getLastMonthEndDate());
本周、上周:
function getWeek(n) {
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth() + 1;
var day = now.getDay(); //返回星期几的某一天;
n = day == 0 ? n + 6 : n + (day - 1)
now.setDate(now.getDate() - n);
let date = now.getDate();
var s = year + "-" + (month < 10 ? ('0' + month) : month) + "-" + (date < 10 ? ('0' + date) : date);
return s;
}
//上周的开始时间
console.log(getWeek(7));
//上周的结束时间
console.log(getWeek(1));
//本周的开始时间
console.log(getWeek(0));
//本周的结束时间
console.log(getWeek(-6));
//下周的开始时间
console.log(getWeek(-7));
//下周结束时间
console.log(getWeek(-13));
好了,常用的日期查询就写到这里希望对大家有帮助!
今天:
//时间戳转日期
function formatMsgTime (timespan) {
var dateTime = new Date(timespan);
var year = dateTime.getFullYear();
var month = dateTime.getMonth() + 1;
var day = dateTime.getDate();
var timeSpanStr;
timeSpanStr = year + '-' + (month < 10 ? ('0' + month) : month) + '-' + (day<10?('0'+day):day);
return timeSpanStr;
}
//今天的时间戳
var noTime=Date.parse(new Date())-1000
formatMsgTime(noTime)//转成日期格式比如:2020-4-20
昨天:
//昨天的时间戳
var time=Date.parse(new Date())-1000*60*60*24*1
formatMsgTime(time)//转成日期格式比如:2020-4-19
本月、上月:
function getMonth(){
var now = new Date(); //当前日期
var nowMonth = now.getMonth(); //当前月
var nowYear = now.getFullYear(); //当前年
var lastMonthDate = new Date(); //上月日期
lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
var lastMonth = lastMonthDate.getMonth();
//格式化日期:yyyy-MM-dd
function formatDate(date) {
var myyear = date.getFullYear();
var mymonth = date.getMonth() + 1;
var myweekday = date.getDate();
if (mymonth < 10) {
mymonth = "0" + mymonth;
}
if (myweekday < 10) {
myweekday = "0" + myweekday;
}
return (myyear + "-" + mymonth + "-" + myweekday);
}
//获得某月的天数
function getMonthDays(myMonth) {
var monthStartDate = new Date(nowYear, myMonth, 1);
var monthEndDate = new Date(nowYear, myMonth + 1, 1);
var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
return days;
}
//获得本月的开始日期
function getMonthStartDate() {
var monthStartDate = new Date(nowYear, nowMonth, 1);
return formatDate(monthStartDate);
}
//获得本月的结束日期
function getMonthEndDate() {
var monthStartDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
return formatDate(monthStartDate);
}
//获得上月开始时间
function getLastMonthStartDate() {
var lastMonthStartDate = new Date(nowYear, lastMonth, 1);
return formatDate(lastMonthStartDate);
}
//获得上月结束时间
function getLastMonthEndDate() {
var lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth));
return formatDate(lastMonthEndDate);
}
return {
getMonthStartDate:getMonthStartDate,
getMonthEndDate:getMonthEndDate,
getLastMonthStartDate:getLastMonthStartDate,
getLastMonthEndDate:getLastMonthEndDate
}
}
//本月开始
console.log(getMonth.getMonthStartDate());
//本月结束
console.log(getMonth.getMonthEndDate());
//上月开始
console.log(getMonth.getLastMonthStartDate());
//上月结束
console.log(getMonth.getLastMonthEndDate());
本周、上周:
function getWeek(n) {
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth() + 1;
var day = now.getDay(); //返回星期几的某一天;
n = day == 0 ? n + 6 : n + (day - 1)
now.setDate(now.getDate() - n);
let date = now.getDate();
var s = year + "-" + (month < 10 ? ('0' + month) : month) + "-" + (date < 10 ? ('0' + date) : date);
return s;
}
//上周的开始时间
console.log(getWeek(7));
//上周的结束时间
console.log(getWeek(1));
//本周的开始时间
console.log(getWeek(0));
//本周的结束时间
console.log(getWeek(-6));
//下周的开始时间
console.log(getWeek(-7));
//下周结束时间
console.log(getWeek(-13));
好了,常用的日期查询就写到这里希望对大家有帮助!
- 上一篇:webstorm找回删除的文件
- 下一篇:js发布订阅者与观察者模式的区别代码示例
精品好课