常规日期间隔计算:开始日期和结束日期要间隔7天、开始时间和结束时间是当前时间、开始日期和结束日期取当前月份的前一个月(以月份计算的)的前一整个月、

1.    // 开始日期和结束日期要间隔7天

      setDefaultDates() {

            const today = new Date();

            const endDate = new Date(today.getTime() - 24 * 60 * 60 * 1000); // 结束日期为查询的前一日

            const startDate = new Date(endDate.getTime() - 6 * 24 * 60 * 60 * 1000); // 开始日期为结束日期的前一周不包括当天

            this.params.endDate = this.formatDate(endDate);

            this.params.startDate = this.formatDate(startDate);

      },

      formatDate(date) {

            return date.toISOString().split('T')[0]; // 格式化日期为 yyyy-MM-dd

      },

2.   // 开始时间和结束时间是当前时间的前一整个月

      getLastMonthRange() {

            let today = new Date();

            let lastMonth = new Date(today.getFullYear(), today.getMonth() - 1, today.getDate());

            // 计算上个月的第一天

            let firstDayLastMonth = new Date(lastMonth.getFullYear(), lastMonth.getMonth(), 1);

            // 计算上个月的最后一天

            let lastDayLastMonth = new Date(lastMonth.getFullYear(), lastMonth.getMonth() + 1, 0);

            // 格式化日期为 yyyy-MM-dd

            let startDate = this.formatDate(firstDayLastMonth);

            let endDate = this.formatDate(lastDayLastMonth);

            return {

                  startDate: startDate,

                  endDate: endDate

                };

          },

        formatDate(date) {

            let d = new Date(date),

            month = '' + (d.getMonth() + 1),

            day = '' + d.getDate(),

            year = d.getFullYear();

            if (month.length < 2)     month = '0' + month;

            if (day.length < 2)    day = '0' + day;

            return [year, month, day].join('-');

      },

3.   // 开始日期和结束日期取当前月份的前一个月(以月份计算的)

      setDefaultDates() {

            const now = new Date();

            const lastMonth = new Date(now.getFullYear(), now.getMonth() - 1, 1);

            this.params.startDate = this.formatDate(lastMonth);

            this.params.endDate = this.formatDate(lastMonth);

      },

      formatDate(date) {

            return date.getFullYear() + '-' + (date.getMonth() + 1).toString().padStart(2, '0');

      },

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容