欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

js处理时间格式

程序员文章站 2022-06-07 18:36:36
...
  • 在项目中有时候需要获取当前的年月日,或者是上月的时间节点,话不多说,直接看代码。
 myDate(dateValue, status) {
            let date;
            //dateValue:有值则是用户自定义时间(element-ui日期选择器)
            if (dateValue) {
                date = new Date(Date.parse(dateValue.replace(/-/g, "/")));
            } else {
                date = new Date();
            }
            let year = date.getFullYear();
            let month = date.getMonth() + 1;
            let lastMon = date.getMonth();
            let day = date.getDate();
            let lastMonthLastDay = new Date(year, lastMon, 0).getDate();
            // status{4:"上月",  3: "本月",2: "本季度", 1: "本年度"}
            if (status == 4) {
                return (
                    year +
                    "年" +
                    lastMon +
                    "月" +
                    1 +
                    "日" +
                    "~" +
                    year +
                    "年" +
                    lastMon +
                    "月" +
                    lastMonthLastDay +
                    "日"
                );
            } else if (status == 3) {
                return (
                    year +
                    "年" +
                    month +
                    "月" +
                    1 +
                    "日" +
                    "~" +
                    year +
                    "年" +
                    month +
                    "月" +
                    day +
                    "日"
                );
            } else if (status == 2) {
                let quarterStartMonth = 0,
                    quarterEndMonth = 0;
                if (month < 4) {
                    quarterStartMonth = 1;
                    quarterEndMonth = 3;
                }
                if (3 < month && month < 7) {
                    quarterStartMonth = 4;
                    quarterEndMonth = 6;
                }
                if (6 < month && month < 10) {
                    quarterStartMonth = 7;
                    quarterEndMonth = 9;
                }
                if (month > 9) {
                    quarterStartMonth = 10;
                    quarterEndMonth = 12;
                }
                return (
                    year +
                    "年" +
                    quarterStartMonth +
                    "月" +
                    1 +
                    "日" +
                    "~" +
                    year +
                    "年" +
                    month +
                    "月" +
                    day +
                    "日"
                );
            } else if (status == 1) {
                return (
                    year +
                    "年" +
                    1 +
                    "月" +
                    1 +
                    "日" +
                    "~" +
                    year +
                    "年" +
                    month +
                    "月" +
                    day +
                    "日"
                );
            } else if (status == 0) {
                return year + "年" + month + "月" + day + "日";
            }
        },
        //函数最终得到的时间格式:xx年xx月xx日~xx年xx月xx日
        ```
相关标签: 实操