|
@@ -0,0 +1,317 @@
|
|
|
|
+/**
|
|
|
|
+ * 获取本周、本季度、本月、上月的开始日期、结束日期
|
|
|
|
+ */
|
|
|
|
+var now = new Date(); //当前日期
|
|
|
|
+var nowDayOfWeek = now.getDay(); //今天本周的第几天
|
|
|
|
+var nowDay = now.getDate(); //当前日
|
|
|
|
+var nowMonth = now.getMonth(); //当前月
|
|
|
|
+var nowYear = now.getYear(); //当前年
|
|
|
|
+nowYear += (nowYear < 2000) ? 1900 : 0; //
|
|
|
|
+var lastMonthDate = new Date(); //上月日期
|
|
|
|
+lastMonthDate.setDate(1);
|
|
|
|
+lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
|
|
|
|
+// var lastYear = lastMonthDate.getYear();
|
|
|
|
+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 getQuarterStartMonth() {
|
|
|
|
+ var quarterStartMonth = 0;
|
|
|
|
+ if (nowMonth < 3) {
|
|
|
|
+ quarterStartMonth = 0;
|
|
|
|
+ }
|
|
|
|
+ if (2 < nowMonth && nowMonth < 6) {
|
|
|
|
+ quarterStartMonth = 3;
|
|
|
|
+ }
|
|
|
|
+ if (5 < nowMonth && nowMonth < 9) {
|
|
|
|
+ quarterStartMonth = 6;
|
|
|
|
+ }
|
|
|
|
+ if (nowMonth > 8) {
|
|
|
|
+ quarterStartMonth = 9;
|
|
|
|
+ }
|
|
|
|
+ return quarterStartMonth;
|
|
|
|
+}
|
|
|
|
+//获得本周的开始日期
|
|
|
|
+function getWeekStartDate() {
|
|
|
|
+ var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek + 1);
|
|
|
|
+ return formatDate(weekStartDate) + ' 00:00:00';
|
|
|
|
+}
|
|
|
|
+//获得本周的结束日期
|
|
|
|
+function getWeekEndDate() {
|
|
|
|
+ var weekEndDate = new Date(nowYear, nowMonth, nowDay + (7 - nowDayOfWeek));
|
|
|
|
+ return formatDate(weekEndDate) + ' 23:59:59';
|
|
|
|
+}
|
|
|
|
+//获得上周的开始日期
|
|
|
|
+function getLastWeekStartDate() {
|
|
|
|
+ var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 7);
|
|
|
|
+ return formatDate(weekStartDate) + ' 00:00:00';
|
|
|
|
+}
|
|
|
|
+//获得上周的结束日期
|
|
|
|
+function getLastWeekEndDate() {
|
|
|
|
+ var weekEndDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 1);
|
|
|
|
+ return formatDate(weekEndDate) + ' 23:59:59';
|
|
|
|
+}
|
|
|
|
+//获得本月的开始日期
|
|
|
|
+function getMonthStartDate() {
|
|
|
|
+ var monthStartDate = new Date(nowYear, nowMonth, 1);
|
|
|
|
+ return formatDate(monthStartDate) + ' 00:00:00';
|
|
|
|
+}
|
|
|
|
+//获得本月的结束日期
|
|
|
|
+function getMonthEndDate() {
|
|
|
|
+ var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
|
|
|
|
+ return formatDate(monthEndDate) + ' 23:59:59';
|
|
|
|
+}
|
|
|
|
+//获得上月开始时间
|
|
|
|
+function getLastMonthStartDate() {
|
|
|
|
+ var lastMonthStartDate = new Date(nowYear, lastMonth, 1);
|
|
|
|
+ return formatDate(lastMonthStartDate) + ' 00:00:00';
|
|
|
|
+}
|
|
|
|
+//获得上月结束时间
|
|
|
|
+function getLastMonthEndDate() {
|
|
|
|
+ var lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth));
|
|
|
|
+ return formatDate(lastMonthEndDate) + ' 23:59:59';
|
|
|
|
+}
|
|
|
|
+//获得本季度的开始日期
|
|
|
|
+function getQuarterStartDate() {
|
|
|
|
+ var quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1);
|
|
|
|
+ return formatDate(quarterStartDate) + ' 00:00:00';
|
|
|
|
+}
|
|
|
|
+//或的本季度的结束日期
|
|
|
|
+function getQuarterEndDate() {
|
|
|
|
+ var quarterEndMonth = getQuarterStartMonth() + 2;
|
|
|
|
+ var quarterStartDate = new Date(nowYear, quarterEndMonth,
|
|
|
|
+ getMonthDays(quarterEndMonth));
|
|
|
|
+ return formatDate(quarterStartDate) + ' 23:59:59';
|
|
|
|
+}
|
|
|
|
+//获得前半年开始时间
|
|
|
|
+function getHalfYearStartDate() {
|
|
|
|
+ var firstHalfYearStartDate = new Date(nowYear, 1 - 1, 1);
|
|
|
|
+ return formatDate(firstHalfYearStartDate) + ' 00:00:00';
|
|
|
|
+}
|
|
|
|
+//获前半年结束时间
|
|
|
|
+function getHalfYearEndDate() {
|
|
|
|
+ var firstHalfYearEndDate = new Date(nowYear, 6 - 1, 30);
|
|
|
|
+ return formatDate(firstHalfYearEndDate) + ' 23:59:59';
|
|
|
|
+}
|
|
|
|
+//获得前今年开始时间
|
|
|
|
+function getThisYearStartDate() {
|
|
|
|
+ var firstThisYearStartDate = new Date(nowYear, 1 - 1, 1);
|
|
|
|
+ return formatDate(firstThisYearStartDate) + ' 00:00:00';
|
|
|
|
+}
|
|
|
|
+//获前今年结束时间
|
|
|
|
+function getThisYearEndDate() {
|
|
|
|
+ var firstThisYearEndDate = new Date(nowYear, 12 - 1, 31);
|
|
|
|
+ return formatDate(firstThisYearEndDate) + ' 23:59:59';
|
|
|
|
+}
|
|
|
|
+//获当前起前一个月时间
|
|
|
|
+function getThisDateBeforMonth() {
|
|
|
|
+
|
|
|
|
+ var lastMonthToday = new Date(
|
|
|
|
+ new Date().getTime() - 30 * 24 * 60 * 60 * 1000
|
|
|
|
+ );
|
|
|
|
+ var lastMonthYear = lastMonthToday.getFullYear();
|
|
|
|
+ let month = lastMonthToday.getMonth() + 1
|
|
|
|
+ var lastMonth = month < 10 ? '0' + month : month;
|
|
|
|
+ var lastMonthDay =
|
|
|
|
+ lastMonthToday.getDate() < 10 ?
|
|
|
|
+ '0' + lastMonthToday.getDate() :
|
|
|
|
+ lastMonthToday.getDate();
|
|
|
|
+ var lastMonthKsrq = lastMonthYear + "-" + lastMonth + "-" + lastMonthDay + " 00:00:00";
|
|
|
|
+ return lastMonthKsrq
|
|
|
|
+}
|
|
|
|
+//获当前起后一个月时间
|
|
|
|
+function getThisDateNextMonth() {
|
|
|
|
+ var nextMonthToday = new Date(
|
|
|
|
+ new Date().getTime() + 30 * 24 * 60 * 60 * 1000
|
|
|
|
+ );
|
|
|
|
+ var nextMonthYear = nextMonthToday.getFullYear();
|
|
|
|
+ let month = nextMonthToday.getMonth() + 1
|
|
|
|
+ var nextMonth = month < 10 ? '0' + month : month;
|
|
|
|
+ var nextMonthDay =
|
|
|
|
+ nextMonthToday.getDate < 10 ?
|
|
|
|
+ "0" + nextMonthToday.getDate :
|
|
|
|
+ nextMonthToday.getDate();
|
|
|
|
+ var nextMonthJsrq = nextMonthYear + "-" + nextMonth + "-" + nextMonthDay;
|
|
|
|
+ return nextMonthJsrq
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// 中国标准时间转年月日时分秒
|
|
|
|
+function timestampToTime(timestamp) {
|
|
|
|
+ // var chinaStandard=Mon Jul 19 2021 11:11:55 GMT+0800 (中国标准时间);
|
|
|
|
+ var date = new Date(timestamp);
|
|
|
|
+ var y = date.getFullYear();
|
|
|
|
+ var m = date.getMonth() + 1;
|
|
|
|
+ m = m < 10 ? ('0' + m) : m;
|
|
|
|
+ var d = date.getDate();
|
|
|
|
+ d = d < 10 ? ('0' + d) : d;
|
|
|
|
+ var h = date.getHours();
|
|
|
|
+ h = h < 10 ? ('0' + h) : h;
|
|
|
|
+ var minute = date.getMinutes();
|
|
|
|
+ minute = minute < 10 ? ('0' + minute) : minute;
|
|
|
|
+ var second = date.getSeconds();
|
|
|
|
+ second = second < 10 ? ('0' + second) : second;
|
|
|
|
+ let time = y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
|
|
|
|
+ return time
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+//获取当前日期yy-mm-dd
|
|
|
|
+//date 为时间对象
|
|
|
|
+function getDateStr3(date) {
|
|
|
|
+ var year = "";
|
|
|
|
+ var month = "";
|
|
|
|
+ var day = "";
|
|
|
|
+ var now = date;
|
|
|
|
+ year = "" + now.getFullYear();
|
|
|
|
+ if ((now.getMonth() + 1) < 10) {
|
|
|
|
+ month = "0" + (now.getMonth() + 1);
|
|
|
|
+ } else {
|
|
|
|
+ month = "" + (now.getMonth() + 1);
|
|
|
|
+ }
|
|
|
|
+ if ((now.getDate()) < 10) {
|
|
|
|
+ day = "0" + (now.getDate());
|
|
|
|
+ } else {
|
|
|
|
+ day = "" + (now.getDate());
|
|
|
|
+ }
|
|
|
|
+ return year + "-" + month + "-" + day;
|
|
|
|
+}
|
|
|
|
+/**
|
|
|
|
+ * 获得相对当前周AddWeekCount个周的起止日期
|
|
|
|
+ * AddWeekCount为0代表当前周 为-1代表上一个周 为1代表下一个周以此类推
|
|
|
|
+ * **/
|
|
|
|
+function getWeekStartAndEnd(AddWeekCount) {
|
|
|
|
+ //起止日期数组
|
|
|
|
+ var startStop = new Array();
|
|
|
|
+ //一天的毫秒数
|
|
|
|
+ var millisecond = 1000 * 60 * 60 * 24;
|
|
|
|
+ //获取当前时间
|
|
|
|
+ var currentDate = new Date();
|
|
|
|
+ //相对于当前日期AddWeekCount个周的日期
|
|
|
|
+ currentDate = new Date(currentDate.getTime() + (millisecond * 7 * AddWeekCount));
|
|
|
|
+ //返回date是一周中的某一天
|
|
|
|
+ var week = currentDate.getDay();
|
|
|
|
+ //返回date是一个月中的某一天
|
|
|
|
+ // var month = currentDate.getDate();
|
|
|
|
+ //减去的天数
|
|
|
|
+ var minusDay = week != 0 ? week - 1 : 6;
|
|
|
|
+ //获得当前周的第一天
|
|
|
|
+ var currentWeekFirstDay = new Date(currentDate.getTime() - (millisecond * minusDay));
|
|
|
|
+ //获得当前周的最后一天
|
|
|
|
+ var currentWeekLastDay = new Date(currentWeekFirstDay.getTime() + (millisecond * 6));
|
|
|
|
+ //添加至数组
|
|
|
|
+ startStop.push(getDateStr3(currentWeekFirstDay));
|
|
|
|
+ startStop.push(getDateStr3(currentWeekLastDay));
|
|
|
|
+
|
|
|
|
+ return startStop;
|
|
|
|
+}
|
|
|
|
+/**
|
|
|
|
+ * 获得相对当月AddMonthCount个月的起止日期
|
|
|
|
+ * AddMonthCount为0 代表当月 为-1代表上一个月 为1代表下一个月 以此类推
|
|
|
|
+ * ***/
|
|
|
|
+function getMonthStartAndEnd(AddMonthCount) {
|
|
|
|
+ //起止日期数组
|
|
|
|
+ var startStop = new Array();
|
|
|
|
+ //获取当前时间
|
|
|
|
+ var currentDate = new Date();
|
|
|
|
+ var month = currentDate.getMonth() + AddMonthCount;
|
|
|
|
+ if (month < 0) {
|
|
|
|
+ var n = parseInt((-month) / 12);
|
|
|
|
+ month += n * 12;
|
|
|
|
+ currentDate.setFullYear(currentDate.getFullYear() - n);
|
|
|
|
+ }
|
|
|
|
+ currentDate = new Date(currentDate.setMonth(month));
|
|
|
|
+ //获得当前月份0-11
|
|
|
|
+ var currentMonth = currentDate.getMonth();
|
|
|
|
+ //获得当前年份4位年
|
|
|
|
+ var currentYear = currentDate.getFullYear();
|
|
|
|
+ //获得上一个月的第一天
|
|
|
|
+ var currentMonthFirstDay = new Date(currentYear, currentMonth, 1);
|
|
|
|
+ //获得上一月的最后一天
|
|
|
|
+ var currentMonthLastDay = new Date(currentYear, currentMonth + 1, 0);
|
|
|
|
+ //添加至数组
|
|
|
|
+ startStop.push(getDateStr3(currentMonthFirstDay));
|
|
|
|
+ startStop.push(getDateStr3(currentMonthLastDay));
|
|
|
|
+ //返回
|
|
|
|
+ return startStop;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 获得当前年月日
|
|
|
|
+ * ***/
|
|
|
|
+function YearMonthDate() {
|
|
|
|
+ var date = new Date();
|
|
|
|
+ var mon = date.getMonth() + 1;
|
|
|
|
+ var day = date.getDate();
|
|
|
|
+ var currDate = date.getFullYear() + "-" + (mon < 10 ? "0" + mon : mon) + "-" + (day < 10 ? "0" + day : day) + " 23:59:59";
|
|
|
|
+ return currDate
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 获得当前年月日SFM
|
|
|
|
+ * ***/
|
|
|
|
+ function YearMonthDateSFN() {
|
|
|
|
+ var date = new Date();
|
|
|
|
+ var mon = date.getMonth() + 1;
|
|
|
|
+ var day = date.getDate();
|
|
|
|
+
|
|
|
|
+ var h = date.getHours();
|
|
|
|
+ h = h < 10 ? ('0' + h) : h;
|
|
|
|
+ var minute = date.getMinutes();
|
|
|
|
+ minute = minute < 10 ? ('0' + minute) : minute;
|
|
|
|
+ var second = date.getSeconds();
|
|
|
|
+ second = second < 10 ? ('0' + second) : second;
|
|
|
|
+
|
|
|
|
+ var currDate = date.getFullYear() + "-" + (mon < 10 ? "0" + mon : mon) + "-" + (day < 10 ? "0" + day : day) + " " + h + ":" + minute + ":" + second;
|
|
|
|
+ return currDate
|
|
|
|
+}
|
|
|
|
+/**
|
|
|
|
+ * 年月日时分秒转时间戳()
|
|
|
|
+ * getTimeFormat('2022-02-02 02:22:22');
|
|
|
|
+ * ***/
|
|
|
|
+function getTimeFormat(timeS){
|
|
|
|
+ let time = (new Date(timeS).getTime()) / 1000; //除1000 是变成秒级的时间戳 不除就是毫秒级
|
|
|
|
+ return time;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+export {
|
|
|
|
+ getWeekStartAndEnd,
|
|
|
|
+ getMonthStartAndEnd,
|
|
|
|
+ getHalfYearStartDate,
|
|
|
|
+ getHalfYearEndDate,
|
|
|
|
+ getThisYearStartDate,
|
|
|
|
+ getThisYearEndDate,
|
|
|
|
+ getWeekStartDate,
|
|
|
|
+ getWeekEndDate,
|
|
|
|
+ getLastWeekStartDate,
|
|
|
|
+ getLastWeekEndDate,
|
|
|
|
+ getMonthStartDate,
|
|
|
|
+ getMonthEndDate,
|
|
|
|
+ getLastMonthStartDate,
|
|
|
|
+ getLastMonthEndDate,
|
|
|
|
+ getQuarterStartDate,
|
|
|
|
+ getQuarterEndDate,
|
|
|
|
+ timestampToTime,
|
|
|
|
+ getThisDateBeforMonth,
|
|
|
|
+ getThisDateNextMonth,
|
|
|
|
+ YearMonthDate,
|
|
|
|
+ YearMonthDateSFN,
|
|
|
|
+ getTimeFormat,
|
|
|
|
+ getDateStr3
|
|
|
|
+}
|