[toc]

时间相关

简单时间格式化

替代 moment.js | Day.js(https://github.com/iamkun/dayjs)

function formatTime(data = {}) {
    const {
        time: time = Date(),
        type: type = 'dateTime',
        dateSeparator: dateSeparator = '-',
        timeSeparator: timeSeparator = ':'
    } = data 
    const timeObj = (typeof time) === 'object' ? time : new Date(time)
    const addZeroCharacter = (n) => n < 10 ? `0${n}` : n

    let y = timeObj.getFullYear(),
        mon = timeObj.getMonth() + 1,
        day = timeObj.getDate(),
        h = timeObj.getHours(),
        m = timeObj.getMinutes(),
        s = timeObj.getSeconds();

    const dateString = `${y}${dateSeparator}${addZeroCharacter(mon)}${dateSeparator}${addZeroCharacter(day)}`
    const timeString = `${addZeroCharacter(h)}${timeSeparator}${addZeroCharacter(m)}${timeSeparator}${addZeroCharacter(s)}`

    const commandObj = {
        dateTimeZh: () => {
            return `${y}${addZeroCharacter(mon)}${addZeroCharacter(day)}${addZeroCharacter(h)}${addZeroCharacter(m)}${addZeroCharacter(s)}`
        },
        date: () => dateString,
        time: () => timeString,
        dateTime: () => `${dateString} ${timeString}`
    }

    return commandObj[type]()
}

获取某个日期是在今年的第几周

/**
 * @des 获取某个日期是在今年的第几周;这里是把 一月一号当作第一周开始然后7天为一个周期,进行计算;有的计算方式是第一周可能不是7天,
 * 可能只有两天,因为前一年有几天是周一周二...,所以两种方式获取的周数可能会有差异
 * @param date: Object (指定某一天或是当天) 
 */
function getWeek(date){
    /* 当前日期 */
    const nowDate = data || new Date();
    /* 构建当年一月一号 00:00:00 日期 */
    const resetFirstDay = new Date(nowDate.getFullYear(), 0, 1);
    /* 获取当前日期到今天相差多少天 */
    const deltaDays = Math.ceil((nowDate.getTime() - resetFirstDay.getTime())/(24*60*60*1000));
    const week = Math.ceil(deltaDays/7);
    return week || 1
}

转换为中文日期格式

/**
 * 转换为中文日期格式
 * t 格式为 2020-03-13 13:36:02
*/
function timeZh(t) {
    var str = t.slice(0,4) + '年'
    str += t.slice(5,7) + '月'
    str += t.slice(8,10) + '日 '
    str += t.slice(11,13) + '时'
    str += t.slice(14,16) + '分'
    str += t.slice(17) + '秒'
    return str;
}

返回中文星期几

/**
 * 返回今天星期几
*/
function weekZh() {
    const week = new Date().getDay()
    const map = new Map([[0, '日'], [1, '一'],[2, '二'],[3, '三'],[4, '四'],[5, '五'],[6, '六']])
    return `星期${map.get(week)}`;
}

返回上午下午

/**
 * 返回上午下午
*/
function timeNowPeriodZh() {
    const now = new Date()
    const hour = now.getHours() 
    let str = ''
    if (hour < 12){
        str='上午'
    } else if (hour < 14){
        str = '中午'
    } else if (hour < 18){
        str = '下午'
    } else if (hour < 23){
        str = '晚上'
    } 
    return str
}

返回当前月份的前n个月组成的数组

/* 返回当前月份的前n个月组成的数组 */
const getMonth = (n = 0, separator = '-') => {
    const time = new Date()
    let y = time.getFullYear()
    let m = time.getMonth()

    const ym = () => (`${y}${separator}${m}`)
    const allM = [ym()]

    for (let i = 0; i < n; i++) {
        m--
        if (m === 0) {
            y--
            m = 12
        }
        allM.push(ym())
    }
    return allM
}
Last Updated:
Contributors: Warren