function formatDate(date) {
let myyear = date.getFullYear();
let mymonth = date.getMonth() + 1;
let myweekday = date.getDate();
if (mymonth < 10) {
mymonth = "0" + mymonth;
}
if (myweekday < 10) {
myweekday = "0" + myweekday;
}
return (myyear + "-" + mymonth + "-" + myweekday + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds());
}
function dateToTimestamp(dateStr = '') {
return Date.parse(new Date(dateStr)) / 1000
}
function timestampToTime(timestamp) {
let date = new Date(timestamp * 1000); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
Y = date.getFullYear() + '-';
M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
D = date.getDate();
return Y + M + D;
}
function timeToString(time) {
let d = new Date(time * 1000); //根据时间戳生成的时间对象
return (d.getFullYear()) + "-" +
(d.getMonth() + 1) + "-" +
(d.getDate()) + " " +
(d.getHours()) + ":" +
(d.getMinutes()) + ":" +
(d.getSeconds())
}
function getMonthDays(myMonth, time) {
let now = new Date(time * 1000); //当前日期
let nowYear = now.getFullYear(); //当前年
let monthStartDate = new Date(nowYear, myMonth, 1);
let monthEndDate = new Date(nowYear, myMonth + 1, 1);
return (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
}
function getLastQuarterStartMonth(time) {
let now = new Date(time * 1000); //当前日期
let nowMonth = now.getMonth(); //当前月
let quarterStartMonth = 0;
if (nowMonth < 3) {
quarterStartMonth = 9;
}
if (2 < nowMonth && nowMonth < 6) {
quarterStartMonth = 0;
}
if (5 < nowMonth && nowMonth < 9) {
quarterStartMonth = 3;
}
if (nowMonth > 8) {
quarterStartMonth = 6;
}
return quarterStartMonth;
}
function getLastWeekStartDate(time) {
let now = new Date(time * 1000); //当前日期
let nowDayOfWeek = now.getDay() - 1; //今天本周的第几天
let nowDay = now.getDate(); //当前日
let nowMonth = now.getMonth(); //当前月
let nowYear = now.getFullYear(); //当前年
return formatDate(new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 7, 0, 0, 0));
}
function getLastWeekEndDate(time) {
let now = new Date(time * 1000); //当前日期
let nowDayOfWeek = now.getDay() - 1; //今天本周的第几天
let nowDay = now.getDate(); //当前日
let nowMonth = now.getMonth(); //当前月
let nowYear = now.getFullYear(); //当前年
return formatDate(new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 1, 23, 59, 59));
}
function getLastDayStartDate(time) {
let now = new Date(time * 1000);
let nowYear = now.getFullYear(); //当前年
month = now.getMonth()
if (month == 11) {
nowYear = nowYear - 1;
}
return formatDate(new Date(nowYear, month, now.getDate() - 1, 0, 0, 0));
}
function getLastDayEndDate(time) {
let now = new Date(time * 1000);
let nowYear = now.getFullYear(); //当前年
month = now.getMonth()
if (month == 11) {
nowYear = nowYear - 1;
}
return formatDate(new Date(nowYear, month, now.getDate() - 1, 23, 59, 59));
}
function getNextDayStartDate(time) {
let now = new Date(time * 1000);
let nowYear = now.getFullYear(); //当前年
month = now.getMonth()
if (month == 11) {
nowYear = nowYear - 1;
}
return formatDate(new Date(nowYear, month, now.getDate() + 1, 0, 0, 0));
}
function getNextDayEndDate(time) {
let now = new Date(time * 1000);
let nowYear = now.getFullYear(); //当前年
month = now.getMonth()
if (month == 11) {
nowYear = nowYear - 1;
}
return formatDate(new Date(nowYear, month, now.getDate() + 1, 23, 59, 59));
}
function getTodayEndDate(time) {
let now = new Date(time * 1000);
let nowYear = now.getFullYear(); //当前年
month = now.getMonth()
if (month == 11) {
nowYear = nowYear - 1;
}
return formatDate(new Date(nowYear, month, now.getDate(), 23, 59, 59));
}
function getMonthStartDate(time) {
let now = new Date(time * 1000); //当前日期
let nowYear = now.getFullYear(); //当前年
month = now.getMonth()
if (month == 11) {
nowYear = nowYear - 1;
}
return formatDate(new Date(nowYear, month, 1, 0, 0, 0));
}
function getMonthEndDate(time) {
let now = new Date(time * 1000); //当前日期
let nowYear = now.getFullYear(); //当前年
let month = now.getMonth();
if (month == 11) {
nowYear = nowYear - 1;
}
return formatDate(new Date(nowYear, month, getMonthDays(month, time), 23, 59, 59));
}
function getLastMonthStartDate(time) {
let now = new Date(time * 1000); //当前日期
let nowYear = now.getFullYear(); //当前年
let lastMonthDate = new Date(time * 1000); //上月日期
lastMonthDate.setDate(1);
lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
let lastMonth = lastMonthDate.getMonth();
if (lastMonth == 11) {
nowYear = nowYear - 1;
}
return formatDate(new Date(nowYear, lastMonth, 1, 0, 0, 0));
}
function getLastMonthEndDate(time) {
let now = new Date(time * 1000); //当前日期
let nowYear = now.getFullYear(); //当前年
let lastMonthDate = new Date(time * 1000); //上月日期
lastMonthDate.setDate(1);
lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
let lastMonth = lastMonthDate.getMonth();
if (lastMonth == 11) {
nowYear = nowYear - 1;
}
return formatDate(new Date(nowYear, lastMonth, getMonthDays(lastMonth, time), 23, 59, 59));
}
function getLastQuarterStartDate(time) {
let now = new Date(time * 1000); //当前日期
let nowYear = now.getFullYear(); //当前年
let quarterStartMonth = getLastQuarterStartMonth(time);
if (quarterStartMonth == 9) {
nowYear = nowYear - 1;
}
return formatDate(new Date(nowYear, quarterStartMonth, 1, 0, 0, 0));
}
function getLastQuarterEndDate(time) {
let now = new Date(time * 1000); //当前日期
let nowYear = now.getFullYear(); //当前年
let quarterStartMonth = getLastQuarterStartMonth(time);
if (quarterStartMonth == 9) {
nowYear = nowYear - 1;
}
return formatDate(new Date(nowYear, quarterStartMonth + 2, getMonthDays(quarterStartMonth + 2, time), 23, 59, 59));
}
function getLastYearStartDate(time) {
let now = new Date(time * 1000); //当前日期
let nowYear = now.getFullYear() - 1; //当前年
return formatDate(new Date(nowYear, 0, 1, 0, 0, 0));
}
function getLastYearEndDate(time) {
let now = new Date(time * 1000); //当前日期
let nowYear = now.getFullYear() - 1; //当前年
return formatDate(new Date(nowYear, 11, 31, 23, 59, 59));
}
function getYearEndDate(time) {
let now = new Date(time * 1000); //当前日期
let nowYear = now.getFullYear(); //当前年
return formatDate(new Date(nowYear, 11, 31, 23, 59, 59));
}
function getNextQuarterStartMonth(time) {
let now = new Date(time * 1000); //当前日期
let nowMonth = now.getMonth(); //当前月
let quarterStartMonth = 0;
if (nowMonth < 3) {
quarterStartMonth = 3;
}
if (2 < nowMonth && nowMonth < 6) {
quarterStartMonth = 6;
}
if (5 < nowMonth && nowMonth < 9) {
quarterStartMonth = 9;
}
if (nowMonth > 8) {
quarterStartMonth = 0;
}
return quarterStartMonth;
}
function getNextWeekStartDate(time) {
let now = new Date(time * 1000); //当前日期
let nowDayOfWeek = now.getDay() - 1; //今天本周的第几天
let nowDay = now.getDate(); //当前日
let nowMonth = now.getMonth(); //当前月
let nowYear = now.getFullYear(); //当前年
return formatDate(new Date(nowYear, nowMonth, nowDay - nowDayOfWeek + 7, 0, 0, 0));
}
function getNextWeekEndDate(time) {
let now = new Date(time * 1000); //当前日期
let nowDayOfWeek = now.getDay() - 1; //今天本周的第几天
let nowDay = now.getDate(); //当前日
let nowMonth = now.getMonth(); //当前月
let nowYear = now.getFullYear(); //当前年
return formatDate(new Date(nowYear, nowMonth, nowDay - nowDayOfWeek + 13, 23, 59, 59));
}
function getNextMonthStartDate(time) {
let now = new Date(time * 1000); //当前日期
let nowYear = now.getFullYear(); //当前年
let lastMonthDate = new Date(time * 1000); //上月日期
lastMonthDate.setDate(1);
lastMonthDate.setMonth(lastMonthDate.getMonth() + 1);
let lastMonth = lastMonthDate.getMonth();
if (lastMonth == 11) {
nowYear = nowYear + 1;
}
return formatDate(new Date(nowYear, lastMonth, 1, 0, 0, 0));
}
function getNextMonthEndDate(time) {
let now = new Date(time * 1000); //当前日期
let nowYear = now.getFullYear(); //当前年
let lastMonthDate = new Date(time * 1000); //上月日期
lastMonthDate.setDate(1);
lastMonthDate.setMonth(lastMonthDate.getMonth() + 1);
let lastMonth = lastMonthDate.getMonth();
if (lastMonth == 11) {
nowYear = nowYear + 1;
}
return formatDate(new Date(nowYear, lastMonth, getMonthDays(lastMonth, time), 23, 59, 59));
}
function getNextQuarterStartDate(time) {
let now = new Date(time * 1000); //当前日期
let nowYear = now.getFullYear(); //当前年
let quarterStartMonth = getNextQuarterStartMonth(time);
if (quarterStartMonth == 0) {
nowYear = nowYear + 1;
}
return formatDate(new Date(nowYear, quarterStartMonth, 1, 0, 0, 0));
}
function getNextQuarterEndDate(time) {
let now = new Date(time * 1000); //当前日期
let nowYear = now.getFullYear(); //当前年
let quarterStartMonth = getNextQuarterStartMonth(time);
if (quarterStartMonth == 0) {
nowYear = nowYear + 1;
}
return formatDate(new Date(nowYear, quarterStartMonth + 2, getMonthDays(quarterStartMonth + 2, time), 23, 59, 59));
}
function getNextYearStartDate(time) {
let now = new Date(time * 1000); //当前日期
let nowYear = now.getFullYear() + 1; //当前年
return formatDate(new Date(nowYear, 0, 1, 0, 0, 0));
}
function getNextYearEndDate(time) {
let now = new Date(time * 1000); //当前日期
let nowYear = now.getFullYear() + 1; //当前年
return formatDate(new Date(nowYear, 11, 31, 23, 59, 59));
}
calculatTime(timestamp) {
let now = new Date().getTime()
let date3 = now - timestamp * 1000; //时间差的毫秒数
//计算出小时数
let leave1 = date3 % (24 * 3600 * 1000) //计算天数后剩余的毫秒数
Hour = Math.floor(leave1 / (3600 * 1000))
//计算相差分钟数
let leave2 = leave1 % (3600 * 1000) //计算小时数后剩余的毫秒数
Minute = Math.floor(leave2 / (60 * 1000))
//计算相差秒数
let leave3 = leave2 % (60 * 1000) //计算分钟数后剩余的毫秒数
Second = Math.round(leave3 / 1000)
}
//实现一个月有几周以及每周开始时间和结束时间
function getweekdata(year, month) { //传入 年 月 获取当月有几周 以及日期
var new_year = year; //取当前的年份
var new_month = month++;//取下一个月的第一天,方便计算(最后一天不固定)
var weekDay = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天"];
if (month > 12) {
new_month -= 12; //月份减
new_year++; //年份增
}
var first_date = new Date(new_year, new_month, 1); //取当年当月中的第一天-时间格式
var last_Data = (new Date(first_date.getTime() - 1000 * 60 * 60 * 24)).getDate() //获取当月最后一天日期
//console.log('最后一天日期'+last_Data+'号')
//当月第一天是周几
var firstzhouji = new Date(new_year + '/' + new_month + '/' + 1).getDay() == 0 ? '星期天' : weekDay[new Date(new_year + '/' + new_month + '/' + 1).getDay() - 1]
//当月最后一天是周几
var lastzhouji = new Date(new_year + '/' + new_month + '/' + last_Data).getDay() == 0 ? '星期天' : weekDay[new Date(new_year + '/' + new_month + '/' + last_Data).getDay() - 1]
var firsttime = '' //第一周有几天
if (firstzhouji == '星期一') { firsttime = 7 }
if (firstzhouji == '星期二') { firsttime = 6 }
if (firstzhouji == '星期三') { firsttime = 5 }
if (firstzhouji == '星期四') { firsttime = 4 }
if (firstzhouji == '星期五') { firsttime = 3 }
if (firstzhouji == '星期六') { firsttime = 2 }
if (firstzhouji == '星期天') { firsttime = 1 }
//console.log('本月第一天“'+firstzhouji+'”本月第一周有“'+firsttime+'”天')
var lasttime = '' //最后一周有几天
if (lastzhouji == '星期一') { lasttime = 1 }
if (lastzhouji == '星期二') { lasttime = 2 }
if (lastzhouji == '星期三') { lasttime = 3 }
if (lastzhouji == '星期四') { lasttime = 4 }
if (lastzhouji == '星期五') { lasttime = 5 }
if (lastzhouji == '星期六') { lasttime = 6 }
if (lastzhouji == '星期天') { lasttime = 7 }
// console.log('本月最后一天“'+lastzhouji+'”本月最后一周有“'+lasttime+'”天')
// 前后两周 加上 剩余周数 得出总周数
var contime = 2 + (last_Data - lasttime - firsttime) / 7
//得出每周对应的日期
var zhouArry = []
for (var i = 1; i <= contime; i++) {
var strTime = ''
var lastTime = ''
if (i == 1) {
strTime = year + '-' + new_month + '-' + 1
lastTime = year + '-' + new_month + '-' + (1 + firsttime - 1)
} else if (i == contime) {
strTime = year + '-' + new_month + '-' + (last_Data - lasttime + 1)
lastTime = year + '-' + new_month + '-' + last_Data
} else {
strTime = addDate(zhouArry[zhouArry.length - 1].endtime, 1)
lastTime = addDate(zhouArry[zhouArry.length - 1].endtime, 7)
}
zhouArry.push({
weeknum: i,
begintime: strTime,
endtime: lastTime,
})
//日期增加 接受两个参数, 传入的时间,传入时间增加的天数
function addDate(date, days) {
if (days == undefined || days == '') {
days = 1;
}
var date = new Date(date);
date.setDate(date.getDate() + days);
var month = date.getMonth() + 1;
var day = date.getDate();
return date.getFullYear() + '-' + month + '-' + day;
}
//console.log(zhouArry)//打印数据
}
return zhouArry
}
js获取指定时间戳的上周、上月、上季度、上年的开始日期、结束日期、相差的时分秒、一个月有几周以及每周开始时间和结束时间
于 2023-03-28 17:59:02 首次发布