vue获取今天、本周、本月、全年的区间时间

943 ℃

1、JS代码如下:

export default {

    //获取今天、昨天、明天
    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;
    },
	
    //获取本周、上周、下周的起始、结束日期
    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;
    },
	
    //获取本月、上月、下月的起始、结束日期
    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;
    },
	
    //获取今年、去年、明年的开始、结束日期
    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;
    }
}

2、JS引用

import dateTime from "@/utils/dateTime";

3、JS代码执行

//当前项目用的是点击触发事件
clickDate(index) {
  if (index === 0) {
	this.date = [dateTime.getDate(0), dateTime.getDate(0)];
  } else if (index === 1) {
	this.date = [dateTime.getMonday("s", 0), dateTime.getMonday("e", 0)];
  } else if (index === 2) {
	this.date = [dateTime.getMonth("s", 0), dateTime.getMonth("e", 0)];
  } else if (index === 3) {
	this.date = [dateTime.getYear("s", 0), dateTime.getYear("e", 0)];
  }
}

el-table合并行的通用方法(附js代码)

vue前端分页功能实现代码(附js代码)

el-table利用:row-style="rowClass"设置指定行变色

一款免费开源效果酷炫​的 Vue / React 大屏数据展示组件库——DataV 

vue项目动态设置background背景色,解决颜色被替换问题

上面是“vue获取今天、本周、本月、全年的区间时间”的全面内容,想了解更多关于 vuejs 内容,请继续关注web建站教程。

当前网址:https://m.ipkd.cn/webs_26.html

声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

全球最受欢迎的前端开源工具库——Bootstrap v4中文文档
js刷新当前页面的4种方法
mysql语法中如何查看/修改用户基本信息(密码/权限等)
用最简单方法实现mysql批量插入数据(for循环)
帝国cms6.6已经确认的七大功能