【案例】js 获取今天/昨天/近一周/一月/一年的日期,时间戳转日期,

// 封装一个公共密码生成函数
// 密码由数字+大小写字母+特殊字符组成
export const GetNewPassword = function GetPassword() {
    var chars = 'ABCDEFGHIJKLMNOPQRSTUVWSYZabcdefghijklmnopqrstuvwsyz0123456789.@$!%*#_~?&^'
    var list = []
    for (var i = 0; i < 8; i++) {
        let val_1 = Math.round(Math.random() * 73)
        list.push(val_1)
    }
    var passwhhd = ''
    for (var n = 0; n < list.length; n++) {
        passwhhd += chars.charAt(list[n])
    }
    // var regNumber = /[A-Za-z0-9]{8}$/
    var regNumber = /^(?![A-Za-z0-9]+$)(?![a-z0-9_!@#$%^&*()+.]+$)(?![A-Za-z_!@#$%^&*()+.]+$)(?![A-Z0-9_!@#$%^&*()+.]+$)[a-zA-Z0-9_!@#$%^&*()+.]{8,}$/
    console.log(regNumber.test(passwd));
    if (regNumber.test(passwd) == true) {
        console.log(passwd);
        return passwd
    } else {
        GetNewPassword()
    }

}
//日期相减
export function subTime(time1, time2) {
    let date1 = new Date(time1.toString()).getTime()   //调用Date函数
    let date2 = new Date(time2.toString()).getTime()   //调用Date函数
    let date = date1 - date2
    let hours = Math.floor(date / 1000 / 3600)
    let minutes = (date / 1000 / 60) % 60

    return hours + "小时" + minutes.toFixed(0) + "分钟"
}
//获取当前日期
export const NowTime = function NowTime() {
    const date = new Date()   //调用Date函数
    const year = date.getFullYear()   //年
    const month = date.getMonth() + 1    //月
    const daily = date.getDate()   //日

    const time = year + '-' + month.toString().padStart(2, "0") + '-' + daily.toString().padStart(2, "0")
    return time
}
// 获取当前时间日期
export const Now_DateTime = function Now_DateTime() {
    const date = new Date()   //调用Date函数
    const year = date.getFullYear()   //年
    const month = date.getMonth() + 1    //月
    const daily = date.getDate()   //日
    const hours = date.getHours()    // 获取-当前小时    15时
    const minutes = date.getMinutes()     //获取---当前分钟    21分
    const seconds = date.getSeconds()    //获取-秒数    53秒
    const datetime = year + '-' + month.toString().padStart(2, "0") + '-' + daily.toString().padStart(2, "0") + ' ' + hours.toString().padStart(2, "0") + ':' + minutes.toString().padStart(2, "0") + ':' + seconds.toString().padStart(2, "0")
    return datetime
}

// 获取当前日期 一年前的日期
export const NowDate_or_lastDate = function name() {
    const date = new Date()   //调用Date函数
    const year = date.getFullYear()   //年
    const month = date.getMonth() + 1    //月
    const daily = date.getDate()   //日
    // const hours = date.getHours()    // 获取-当前小时    15时
    // const minutes = date.getMinutes()     //获取---当前分钟    21分
    // const seconds = date.getSeconds()    //获取-秒数    53秒
    const NOWdatetime = year + '-' + month.toString().padStart(2, "0") + '-' + daily.toString().padStart(2, "0");
    const LASTdatetime = year - 1 + '-' + month.toString().padStart(2, "0") + '-' + daily.toString().padStart(2, "0")
    return [NOWdatetime + ' 00:00:00', LASTdatetime + ' 23:59:59']
}

//获取昨天日期
export const Yesterday = function Yesterday() {
    const date = new Date();
    date.setTime(date.getTime() - 3600 * 1000 * 24);
    const year = date.getFullYear(); //年
    const month = date.getMonth() + 1; //月
    const daily = date.getDate(); //日

    const time = year + '-' + month.toString().padStart(2, "0") + '-' + daily.toString().padStart(2, "0")
    return time
}
// 
//获取当前日期的前一个月日期
export const oldTime = function oldTime() {
    const date = new Date();
    date.setTime(date.getTime() - 3600 * 1000 * 24 * 30);
    const year = date.getFullYear()   //年
    const month = date.getMonth() + 1   //月
    const daily = date.getDate()   //日

    const oldtime = year + '-' + month.toString().padStart(2, "0") + '-' + daily.toString().padStart(2, "0")
    return oldtime
}
//获取当前日期的前一个周日期
export const oldweekTime = function oldweekTime() {
    const date = new Date();
    date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
    const year = date.getFullYear()   //年
    const month = date.getMonth() + 1   //月
    const daily = date.getDate()   //日

    const oldtime = year + '-' + month.toString().padStart(2, "0") + '-' + daily.toString().padStart(2, "0")
    return oldtime
}
//获取本月日期
export const NowMonthTime = function NowMonthTime() {
    var date = new Date()   //调用Date函数
    var year = date.getFullYear()   //年
    var month = date.getMonth() + 1    //月
    var daily = ''
    if (year / 4 == 0) {
        if (month == 2) {
            if (month < 10) {
                month = '0' + month
            }
            daily = 29
        }
    } else {
        if (month < 10) {
            month = '0' + month
        }
        daily = 30
    }

    let time = year + '-' + month + '-' + daily
    return time
}
// 获取近12个月的数组
export function get12Months() {
    let dataArr = [];
    let data = new Date();
    data.setMonth(data.getMonth() + 1, 1); //获取到当前月份,设置月份
    for (let i = 0; i < 12; i++) {
        data.setMonth(data.getMonth() - 1); //每次循环一次 月份值减1
        let m = data.getMonth() + 1;
        // m = m < 10 ? "0" + m : m;
        dataArr.push(data.getFullYear() + "_" + m);
    }
    return dataArr;
}
// 获取近十二月的月份
export function get12Months1() {
    let dataArr = [];
    let data = new Date();
    data.setMonth(data.getMonth() + 1, 1); //获取到当前月份,设置月份
    for (let i = 0; i < 12; i++) {
        data.setMonth(data.getMonth() - 1); //每次循环一次 月份值减1
        let m = data.getMonth() + 1;
        m = m < 10 ? "0" + m : m;
        dataArr.push(data.getFullYear() + "-" + m);
    }
    return dataArr;
}
// 时间戳转日期格式:2022-07-07 12:12:12
export const timestampToTime = function timestampToTime(timestamp) {
    var date = new Date(timestamp);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = date.getDate();
    var h = date.getHours() + ':';
    var m = date.getMinutes() + ':';
    var s = date.getSeconds();
    return Y + M + D.toString().padStart(2, '0') + ' ' + h.padStart(3, '0') + m.padStart(3, '0') + s.toString().padStart(2, '0');
}

你可能感兴趣的:(js,案例,javascript,前端)