js-时间相关操作(获取当前日期、最近三天、近一月及前N天的日期)

项目中,总有和时间日期相关的处理,因此为了方便自己和你们,我就做一个妥善的整理。

我这里采用vue3.0的写法

1.日期选择器,限制范围(默认当前日期,以及前2天)
js-时间相关操作(获取当前日期、最近三天、近一月及前N天的日期)_第1张图片

<template>
 <span>选择时间:</span>
          <el-date-picker
            v-model="licenseEndDate"
            type="daterange"
            range-separator="-"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
            start-placeholder="请选择开始时间"
            end-placeholder="请选择结束时间"
            :disabledDate="disabledDate"
            @change="onChangeDateRange"
          />
</template>

<script setup>
import {
  onBeforeUnmount,
  onMounted,
  reactive,
  ref,
  getCurrentInstance,
  toRefs,
  watch,
} from "vue";

const licenseEndDate = ref([]);

onMounted(() => {

  licenseEndDate.value = [
    moment(new Date().getTime() - 2 * 1000 * 24 * 60 * 60).format("YYYY-MM-DD"),
    moment(new Date()).format("YYYY-MM-DD"),
  ];
});

//限制只能选择当前日期之前
const disabledDate = (time) => {
  return time.getTime() > Date.now();
};
</sc
ript>

2.最近三天、近一月及前N天的日期


const getData=(value)=>{
//value---最近天数
 moment(new Date().getTime() - value * 1000 * 24 * 60 * 60).format('YYYY-MM-DD')
}

3.获取今日/昨日/本周/上周/本月/上月 时间

  // 获取今日的开始结束时间
   export const getToday=()=> {
        let obj = {
            starttime: '',
            endtime: ''
        }
        obj.starttime = moment(moment().startOf("day").valueOf()).format("YYYY-MM-DD HH:mm:ss");
        obj.endtime = moment(moment().valueOf()).format("YYYY-MM-DD HH:mm:ss");
        return obj
    },
    // 获取昨日的开始结束时间
  export  const getYesterday=()=> {
        let obj = {
            starttime: '',
            endtime: ''
        }
        obj.starttime = moment(moment().add(-1, 'days').startOf("day").valueOf()).format("YYYY-MM-DD HH:mm:ss");
        obj.endtime = moment(moment().add(-1, 'days').endOf('day').valueOf()).format('YYYY-MM-DD HH:mm:ss');
        return obj
    },
    // 获取当前周的开始结束时间
    export  const getCurrWeekDays=()=> {
        let obj = {
            starttime: '',
            endtime: ''
        }
        obj.starttime = moment(moment().week(moment().week()).startOf('week').add(1, 'days').valueOf()).format('YYYY-MM-DD HH:mm:ss')
        obj.endtime = moment(moment().week(moment().week()).endOf('week').add(1, 'days').valueOf()).format('YYYY-MM-DD HH:mm:ss');
        return obj
    },
    // 获取上一周的开始结束时间
  export  const getLastWeekDays=()=> {
        let obj = {
            starttime: '',
            endtime: ''
        }
        obj.starttime = moment(moment().week(moment().week() - 1).startOf('week').add(1, 'days').valueOf()).format('YYYY-MM-DD HH:mm:ss')
        obj.endtime = moment(moment().week(moment().week() - 1).endOf('week').add(1, 'days').valueOf()).format('YYYY-MM-DD HH:mm:ss');
        return obj
    },
    // 获取当前月的开始结束时间
    export  const getCurrMonthDays=() =>{
        let obj = {
            starttime: '',
            endtime: ''
        }
        obj.starttime = moment(moment().month(moment().month()).startOf('month').valueOf()).format('YYYY-MM-DD HH:mm:ss');
        obj.endtime = moment(moment().month(moment().month()).endOf('month').valueOf()).format('YYYY-MM-DD HH:mm:ss');
        return obj
    },
    // 获取上一个月的开始结束时间
 export  const getLastMonthDays=()=> {
        let obj = {
            starttime: '',
            endtime: ''
        }
        obj.starttime = moment(moment().month(moment().month() - 1).startOf('month').valueOf()).format('YYYY-MM-DD HH:mm:ss');
        obj.endtime = moment(moment().month(moment().month() - 1).endOf('month').valueOf()).format('YYYY-MM-DD HH:mm:ss');
        return obj
    },

4.时间戳(获取系统时间)

const timeFilter=()=> {
				var date = new Date();
				var times = date.getHours();
				var whe = parseInt(times);
				var y = date.getFullYear()
				var m = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1)
				var d = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate())
				var startTime = y + "-" + m + "-" + d;
				return startTime;
			}

/**
 * 当前时间
 * @param {*} date 
 */
function DateTime(date) {
    if (date) {
        return new Date(date);
    }
    return new Date();
}

/**
 * 获取日期 yyyy-MM-dd 00:00:00
 */
function GetDate() {
    var date = this.ToDateString();
    date = DateTime(date);
    var h = date.ToString("HH");
    if (h > 0) {
        date = DateTime(date);
        date = date.AddH(0 - h);
    }
    return date
}

/**
 * 当前时间字符串
 */
function ToString(fmt) {
    var date = this;
    fmt = fmt || "yyyy/MM/dd HH:mm:ss"
    return dateFormat(fmt, date);
}

/**
 * 当前日期字符串
 */
function ToDateString() {
    var date = this;
    return dateFormat("yyyy-MM-dd", date);
}

/**
 * 增加一天
 */
function AddDay(num) {
    var date = this;
    date = date.setDate(date.getDate() + num);
    return new Date(date);
}

/**
 * 增加一周
 */
function AddWeek(num) {
    var date = this;
    date = date.setDate(date.getDate() + (num * 7));
    return new Date(date);
}

/**
 * 增加一个月
 */
function AddMonth(num) {
    var date = this;
    date = date.setMonth(date.getMonth() + num);
    return new Date(date);
}

/**
 * 增加一年
 */
function AddYear(num) {
    var date = this;
    date = date.setFullYear(date.getFullYear() + num);
    return new Date(date);
}


/**
 * 增加一小时
 */
function AddH(num) {
    var date = this;
    date = date.setHours(date.getHours() + (num));
    return new Date(date);
}

/**
 * 增加一分钟
 */
function AddM(num) {
    var date = this;
    date = date.setMinutes(date.getMinutes() + (num));
    return new Date(date);
}

/**
 * 增加一秒
 */
function AddS(num) {
    var date = this;
    date = date.setSeconds(date.getSeconds() + (num));
    return new Date(date);
}

/**
 * 增加一毫秒
 */
function AddF(num) {
    var date = this;
    date = date.setMilliseconds(date.getMilliseconds() + (num));
    return new Date(date);
}

/**
 * 月份天数
 */
function MDays() {
    var date = this;
    var d = new Date(date.ToString("y"), date.ToString("M"), 0);
    return d.getDate();
}

/**
 * 格式化时间
 */
function dateFormat(fmt, date) {
    var ret;
    var opt = {
        "y+": date.getFullYear().toString(), // 年
        "M+": (date.getMonth() + 1).toString(), // 月
        "d+": date.getDate().toString(), // 日
        "H+": date.getHours().toString(), // 时
        "m+": date.getMinutes().toString(), // 分
        "s+": date.getSeconds().toString(), // 秒
        "f+": date.getMilliseconds().toString() // 毫秒
            // 有其他格式化字符需求可以继续添加,必须转化成字符串
    };
    for (var k in opt) {
        ret = new RegExp("(" + k + ")").exec(fmt);
        if (ret) {
            fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
        };
    };
    return fmt;
}

/**
 * 计算时间差
 */
function dateDiff(startDate, EndDate) {
    var dateStart = new Date(startDate);
    var dateEnd = new Date(EndDate);
    return Value = {
        day: parseInt((dateEnd - dateStart) / (1000 * 60 * 60 * 24)),
        Hours: parseInt((dateEnd - dateStart) / (1000 * 60 * 60)),
        Minutes: parseInt((dateEnd - dateStart) / (1000 * 60)),
        Seconds: parseInt((dateEnd - dateStart) / (1000)),
        t: parseInt((dateEnd - dateStart)),
    };
}


/**
 * 获取本周所有日期 字符串数组
 */
function GetWeekDays() {
    var days = [];
    var date = this;
    var today = date.getDay();
    var Sunday = date.AddDay(-today);
    for (var i = 0; i < 7; i++) {
        days.push(Sunday.AddDay(i).ToString("yyyy/MM/dd"));
        Sunday.AddDay(-i);
    }
    return days;
}

/**
 * 获取本月所有日期 字符串数组
 */
function GetMonthDays(n, b) {
    var weeks = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
    n = n || 0;
    var days = [];
    var date = this;
    var newDate = date.AddMonth(n);
    var M = newDate.ToString("M");
    var W = newDate.getDay();
    var week = weeks[newDate.getDay()];
    var m_days = newDate.MDays();
    var strDate = new Date(newDate.ToString("yyyy-MM")).AddDay(-1);
    // console.log(strDate.ToString("yyyy-MM-dd"))
    for (var i = 0; i < m_days; i++) {
        var $day = strDate.AddDay(1);
        var _day = $day.ToString("yyyy-MM-dd");
        var _w = $day.getDay();
        var _week = weeks[$day.getDay()];
        days.push({
            day: _day, //日期
            week: _week, //星期,中文
            w: _w, //星期几,数字值,星期天为0
            dy: true, //是否为当月日期
            y: $day.ToString("yyyy"), //年
            m: $day.ToString("MM"), //月
            d: $day.ToString("dd"), //日
        });
    }

    if (b) {
        var day_1 = days[0];
        var c_1 = -(0 - day_1.w);
        if (day_1.w > 0) {
            // console.log(day_1);
            for (var i = 0; i < c_1; i++) {
                var _s = DateTime(day_1.day);
                var _d = _s.AddDay(-i - 1);
                var _w = _s.getDay();
                var _week = weeks[_s.getDay()];
                days.unshift({
                    day: _d.ToString("yyyy-MM-dd"),
                    week: _week,
                    w: _w,
                    dy: false,
                    y: _d.ToString("yyyy"), //年
                    m: _d.ToString("MM"), //月
                    d: _d.ToString("dd"), //日
                });
            }
        }
        var day_2 = days[days.length - 1];
        console.log(day_2)
        if (day_2.w < 6) {
            var c_2 = -(day_2.w - 6);
            for (var i = 0; i < c_2; i++) {
                var _s = DateTime(day_2.day);
                var _d = _s.AddDay(i + 1);
                var _w = _s.getDay();
                var _week = weeks[_s.getDay()];
                days.push({
                    day: _d.ToString("yyyy-MM-dd"),
                    week: _week,
                    w: _w,
                    dy: false,
                    y: _d.ToString("yyyy"), //年
                    m: _d.ToString("MM"), //月
                    d: _d.ToString("dd"), //日
                });
                // days.push(null);
            }
        }
    }
    return days;
}

/**
 * 获取时间对象
 * @returns
 */
function ToObj() {
    var _self = this;
    return {
        year: Number(_self.getFullYear().toString()),
        Month: Number((_self.getMonth() + 1).toString()),
        Day: Number(_self.getDate().toString()),
        Hours: Number(_self.getHours().toString()),
        Minutes: Number(_self.getMinutes().toString()),
        Seconds: Number(_self.getSeconds().toString()),
        Milliseconds: Number(_self.getMilliseconds().toString()),
    };
}

/**
 * 获取当前日期是星期几 
 */
function ToWeek() {
    var weeks = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
    return weeks[this.getDay()];
}

Date.prototype.ToString = ToString; //时间转字符串
Date.prototype.ToDateString = ToDateString; //时间转字符串
Date.prototype.AddDay = AddDay; //增加天
Date.prototype.AddWeek = AddWeek; //增加周
Date.prototype.AddMonth = AddMonth; //增加月
Date.prototype.AddYear = AddYear; //增加年
Date.prototype.AddH = AddH; //增加小时
Date.prototype.AddM = AddM; //增加分
Date.prototype.AddS = AddS; //增加秒
Date.prototype.AddF = AddF; //增加毫秒
Date.prototype.GetWeekDays = GetWeekDays; //获取本周所有日期
Date.prototype.GetMonthDays = GetMonthDays; //获取本周所有日期
Date.prototype.MDays = MDays; //获取本月天数
Date.prototype.GetDate = GetDate; //日期
Date.prototype.ToObj = ToObj; //获取时间对象
Date.prototype.ToWeek = ToWeek; //获取当前日期是星期几

/**---时间字符串操作--------------------------------------------------- */
/**
 * /Date(1591070400000)/ 格式时间转时间对象
 */
function ToDate() {
    var d = eval('new ' + this.substr(1, this.length - 2));
    return d;
}

String.prototype.ToDate = ToDate;



/**
 * String.padStart()
 */
if (!String.prototype.padStart) {
    String.prototype.padStart = function padStart(targetLength, padString) {
        targetLength = targetLength >> 0; //floor if number or convert non-number to 0;
        padString = String(typeof padString !== 'undefined' ? padString : ' ');
        if (this.length > targetLength) {
            return String(this);
        } else {
            targetLength = targetLength - this.length;
            if (targetLength > padString.length) {
                padString += padString.repeat(targetLength / padString.length); //append to original to ensure we are longer than needed
            }
            return padString.slice(0, targetLength) + String(this);
        }
    };
}

你可能感兴趣的:(Js,Vue,Vue3.0,javascript,前端,开发语言)