js日期時間工具類收集

獲取指定日期範圍

function timeLimit(e) {
    let now = new Date(); //當前日期
    let nowDayOfWeek = now.getDay(); //今天本週的第幾天
    let nowDay = now.getDate(); //當前日
    let nowMonth = now.getMonth(); //當前月
    let nowYear = now.getFullYear(); //當前年份
    let arr = [];
    if (e === '0') {
        arr[0] = `${nowYear}-${nowMonth+1}-${nowDay}`;
        arr[1] = `${nowYear}-${nowMonth+1}-${nowDay}`;
    } else if (e === '1') { //本月
        arr[0] = getMonthStartDate(nowYear, nowMonth);
        arr[1] = getMonthEndDate(nowYear, nowMonth);
    } else if (e === '2') { //本週
        arr[0] = getWeekStartDate(nowYear, nowMonth, nowDay, nowDayOfWeek);
        arr[1] = getWeekEndDate(nowYear, nowMonth, nowDay, nowDayOfWeek);
    } else if (e === '3') { //上月
        if (nowMonth === 0) {
            arr[0] = getMonthStartDate(nowYear - 1, 11);
            arr[1] = getMonthEndDate(nowYear - 1, 11);
        } else {
            arr[0] = getMonthStartDate(nowYear, nowMonth - 1);
            arr[1] = getMonthEndDate(nowYear, nowMonth - 1);
        }
    } else if (e === '4') { //三個月
        let interval = 2 - (nowMonth + 1);
        if (interval < 0) {
            arr[0] = getMonthStartDate(nowYear, nowMonth - 2);
            arr[1] = getMonthEndDate(nowYear, nowMonth);
        } else {
            arr[0] = getMonthStartDate(nowYear - 1, 11 - interval);
            arr[1] = getMonthEndDate(nowYear, nowMonth);
        }
    } else if (e === '5') { //半年
        let interval = 5 - (nowMonth + 1);
        if (interval < 0) {
            arr[0] = getMonthStartDate(nowYear, nowMonth - 5);
            arr[1] = getMonthEndDate(nowYear, nowMonth);
        } else {
            arr[0] = getMonthStartDate(nowYear - 1, 11 - interval);
            arr[1] = getMonthEndDate(nowYear, nowMonth);
        }
    }
    return arr;
}
//格式化日期  
function formatDate(date) {
    let theYear = date.getFullYear();
    let theMonth = date.getMonth() + 1;
    let theDay = date.getDate();
    theMonth = theMonth < 10 ? "0" + theMonth : theMonth;
    theDay = theDay < 10 ? "0" + theDay : theDay;
    return (theYear + "-" + theMonth + "-" + theDay);

}
//得到某月的天數  
function getMonthDays(year, month) {
    let monthStartDate = new Date(year, month, 1);
    let monthEndDate = new Date(year, month + 1, 1);
    return (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
}
//得到本月的開始日期 
function getMonthStartDate(year, month) {
    let monthStartDate = new Date(year, month, 1);
    return formatDate(monthStartDate);

}
//得到本月的結束日期  
function getMonthEndDate(year, month) {
    let monthEndDate = new Date(year, month, getMonthDays(year, month));
    return formatDate(monthEndDate);
}
//得到本週的開始日期  
function getWeekStartDate(year, month, day, week) {
    let weekStartDate = new Date(year, month, day - week);
    return formatDate(weekStartDate);

}
//得到本週的結束日期  
function getWeekEndDate(year, month, day, week) {
    let weekEndDate = new Date(year, month, day + (6 - week));
    return formatDate(weekEndDate);
}

new Date轉化爲yyyy-MM-dd HH:mm:ss

let date = new Date()
date.toLocaleString()  // 2020/7/9 上午9:30:27
//轉化爲dd-MM-yyyy HH:mm:ss
let time = date.toLocaleString("en-US", { hour12: false }).replace(/\b\d\b/g, '0$&').replace(new RegExp('/','gm'),'-')    //07-09-2020, 09:30:27
//轉化爲yyyy-MM-dd HH:mm:ss
let time = date.toLocaleString("zh-cn", { hour12: false }).replace(/\b\d\b/g, '0$&').replace(new RegExp('/','gm'),'-') // 2020-07-09 09:30:27

let time = date.toLocaleString("zh-cn", { hour12: true }).replace(/\b\d\b/g, '0$&').replace(new RegExp('/','gm'),'-') // 2020-07-09 上午09:30:27

將yyyy-MM-dd轉化爲new Date()

new Date('2020-07-09 9:44:00');

獲取當前的時間yyyy-MM-dd HH:mm:ss

let date = new Date();
let year = date.getFullYear();
let month = date.getMonth() + 1;
let day=date.getDate();
let hours=date.getHours();
let minu=date.getMinutes();
let second=date.getSeconds();
//判斷是否滿10
let arr=[month,day,hours,minu,second];
arr.forEach(item=>{
item< 10?"0"+item:item;
})
let dataFormat = year+'-'+arr[0]+'-'+arr[1]+' '+arr[2]+':'+arr[3]+':'+arr[4]

將時間戳轉化爲yyyy-MM-dd HH:mm:ss

let middleDate=new Date(1594352983166);
let dateFormat = middleDate.toLocaleString('zh-CN',{hour12:false}).replace(/\b\d\b/g, '0$&').replace(new RegExp('/','gm'),'-')

比較yyyy-MM-dd時間大小

export default const compareTwo=(dateOne,dateTwo)=>{
    return Number(dateOne.replace(/\-/g,""))<Number(dateTwo.replace(/\-/g,""))
}
compareTwo('2020-07-01','2020-06-04')  //false

計算兩個日期格式爲(yyyy-MM-dd)相差幾個月

export default const disparityFewMonth = (dateOne, dateTwo) => {
    let datesOne = dateOne.split('-').map(item => Number(item));
    let datesTwo = dateTwo.split('-').map(item => Number(item));
    const diff = [0, 0, 0].map((value, index) => {
        return datesOne[index] - datesTwo[index]
    });
    return (diff[0] * 12 + diff[1]) + '月' + diff[2] + '天'
}
disparityFewMonth('2020-09-03','2020-03-03')//"6月0天"
disparityFewMonth('2020-04-03','2020-05-09')//"-1月-6天"
相關文章
相關標籤/搜索