js時間處理

1、秒轉(zhuǎn)換成 時:分:秒

export const formatSeconds = (value:number)=>{
    var secondTime = parseInt(value);// 秒
    var minuteTime = 0;// 分
    var hourTime = 0;// 小時
    if(secondTime > 60) {//如果秒數(shù)大于60,將秒數(shù)轉(zhuǎn)換成整數(shù)
        //獲取分鐘,除以60取整數(shù),得到整數(shù)分鐘
        minuteTime = parseInt(secondTime / 60);
        //獲取秒數(shù),秒數(shù)取佘,得到整數(shù)秒數(shù)
        secondTime = parseInt(secondTime % 60);
        //如果分鐘大于60,將分鐘轉(zhuǎn)換成小時
        if(minuteTime > 60) {
            //獲取小時,獲取分鐘除以60,得到整數(shù)小時
            hourTime = parseInt(minuteTime / 60);
            //獲取小時后取佘的分,獲取分鐘除以60取佘的分
            minuteTime = parseInt(minuteTime % 60);
        }
    }else{
        return "00:" + secondTime
    }
    var result = "" + parseInt(secondTime) ;

    if(minuteTime > 0) {
        result = "" + parseInt(minuteTime) + ":" + result;
    }
    if(hourTime > 0) {
        result = "" + parseInt(hourTime) + ":" + result;
    }
    return result;
}

2、時間戳轉(zhuǎn)為 幾天(小時、分鐘)前、剛剛

export const timeFormat = (time:number| string) => {
    let one_minutes = 60 //1分鐘 60秒
    let one_hours = 3600  //1小時 3600秒
    let one_day = 86400    //1天 86400秒

    let cur_date = new Date()
    let old_date = new Date(time)

    let cur_years = cur_date.getFullYear()//現(xiàn)在的年份
    let old_years = old_date.getFullYear()//比較的年份
    let new_datetime = cur_date.getTime(); //現(xiàn)在的時間
    let old_datetime = old_date.getTime(); //過去比較的一個時間點
    let difftime = Math.floor((new_datetime - old_datetime)/1000) //相差秒數(shù)
    let minutes = Math.floor(difftime/one_minutes); // 相差分鐘 60秒
    let hours = Math.floor(difftime/one_hours);    // 相差小時 60*60 秒
    let days = Math.floor(difftime/one_day); // 相差天  24*60*60 秒

    let timeTxt = ''
    if(difftime<60){
        //1分鐘內(nèi)
        timeTxt = G.LANG.time_just_now
    }
    if(difftime>=60 && difftime< one_hours){
        //60分鐘內(nèi)
        timeTxt = mo.STR.placeholder(G.LANG.time_minute_ago,{num:minutes})
    }
    if(difftime>=one_hours && difftime<one_hours*24){
        //24小時內(nèi)
        timeTxt = mo.STR.placeholder(G.LANG.time_hour_ageo,{num:hours})
    }
    if(difftime>=one_hours*24 && difftime<one_hours*24*3){
        //24-48小時內(nèi) 48-72小時內(nèi)
        timeTxt = mo.STR.placeholder(G.LANG.time_days_ago,{num:days})
    }
    // if(difftime>=3600*24*2 && difftime<3600*24*2){
    //     //48-72小時內(nèi)
    //     timeTxt = '2天前'
    // }
    if(difftime>=one_hours*24*3){
        //48-72小時內(nèi)
       let showYear = cur_years != old_years
        timeTxt = timestampToDay(time,showYear)
    }
    return timeTxt
}
/**
 * 動態(tài)時間
 */
 function timestampToDay(timestamp:number| string,showYear:boolean=true) {
    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() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
    return `${showYear?Y:''}${M}${D}`
    // return Y + M + D ;
}

3、日期轉(zhuǎn)周

getWeekStr(date){
        let weekNum = date.getDay()
        if(weekNum === 0){
            return '周日'
        }
        if(weekNum === 1){
            return '周一'
        }
        if(weekNum === 2){
            return '周二'
        }
        if(weekNum === 3){
            return '周三'
        }
        if(weekNum === 4){
            return '周四'
        }
        if(weekNum === 5){
            return '周五'
        }
        if(weekNum === 6){
            return '周六'
        }

    }

4、date轉(zhuǎn)指定格式,不如 MM-dd

formatDate(date,fmt){
        var o = {
            "M+": date.getMonth() + 1, //月份
            "d+": date.getDate(), //日
            "h+": date.getHours(), //小時
            "m+": date.getMinutes(), //分
            "s+": date.getSeconds(), //秒
            "q+": Math.floor((date.getMonth() + 3) / 3), //季度
            "S": date.getMilliseconds() //毫秒
        };
        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt))
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;

    }

5、使用moment獲取時間

import moment from "moment/moment";
export const toYear = (moment(new Date()).format('YYYY'))
export const toMonth = (moment(new Date()).format('MM'))
export const toDay = (moment(new Date()).format('DD'))

6、獲取當月的天數(shù)

//獲取當月的天數(shù)
export const getDaysOfMonth = (year, month) => {
    var day = new Date(year, month, 0)
    var dayCount = day.getDate()
    return dayCount
}

7、獲取幾天前后的日期

 // 2.獲取幾天之前或幾天之后的日期 傳入date和天數(shù)
    getBeforeDayDate(date,day) {
        let timestamp = date.getTime();
        // 獲取day天前的日期
        return new Date(timestamp - day * 24 * 3600 * 1000);
    }

    getAfterDayDate(date,day) {
        let timestamp = date.getTime();
        // 獲取day天之后的日期
        return new Date(timestamp + day * 24 * 3600 * 1000);
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容