溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

javascript常用時(shí)間函數(shù)集合

發(fā)布時(shí)間:2020-05-30 19:26:47 來(lái)源:網(wǎng)絡(luò) 閱讀:313 作者:Lee_吉 欄目:web開(kāi)發(fā)
  1. 代碼:
    /*
    @desc:時(shí)間日期函數(shù)集合
    @author [Lee] <[<complet@163.com>]>
    */
    function datetime(){
    /*
     @desc:內(nèi)部方法,不足10補(bǔ)0
     @param input 傳入數(shù)值
     @return ret 轉(zhuǎn)換后的數(shù)值
     */
    this.parsetime = function(input){
        var ret
        if(input >= 0 && input < 10){
            ret = '0'+input
        }else{
            ret = input
        }
        return ret
    }
    /*
     @desc:時(shí)間綴轉(zhuǎn)時(shí)間
     @param time 時(shí)間綴,10位
     @return ret 時(shí)間,格式為:2018-01-01 00:00:00
     */ this.timetostr = function(time){
        var time = (time)?(time):(Math.round(new Date().getTime()/1000))
        var date = new Date(time*1000)
        var year = this.parsetime(date.getFullYear())
        var month = this.parsetime(date.getMonth()+1)
        var day = this.parsetime(date.getDate())
        var hour = this.parsetime(date.getHours())
        var minute = this.parsetime(date.getMinutes())
        var second = this.parsetime(date.getSeconds())
        var ret = year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second
        return ret
    }
    /*
     @desc:時(shí)間轉(zhuǎn)時(shí)間綴
     @param str 時(shí)間字符串
     @return time 時(shí)間綴
     */
    this.strtotime = function(str){
        var date = new Date(str)
        var year = this.parsetime(date.getFullYear())
        if(/^\d{4}(-\d{2}(-\d{2})?)?\s\d{2}:\d{2}(:)?(\d{2})?$/.test(str)){
            var month = this.parsetime(date.getMonth()+1)
            var day = this.parsetime(date.getDate())
            var hour = this.parsetime(date.getHours())
            var minute = this.parsetime(date.getMinutes())
            var second = this.parsetime(date.getSeconds())
        }else{
            var month = this.parsetime(date.getUTCMonth()+1)
            var day = this.parsetime(date.getUTCDate())
            var hour = this.parsetime(date.getUTCHours())
            var minute = this.parsetime(date.getUTCMinutes())
            var second = this.parsetime(date.getUTCSeconds())
        }
        str = year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second
        date = new Date(str)
        var time = date.getTime().toString()
        time = time.slice(0,10)
        return time
    }
    /*
     @desc:求時(shí)間差
     @param sstr 開(kāi)始時(shí)間,字符串
     @param estr 結(jié)束時(shí)間,字符串
     @return ret 時(shí)間差,單位:秒
     */
    this.timediff = function(sstr,estr){
        var stime = this.strtotime(sstr)
        var etime = this.strtotime(estr)
        var ret = etime - stime
        return ret
    }
    /*
     @desc:時(shí)間相加函數(shù)
     @param dur 差數(shù),如:-1year +2month
     @param time 時(shí)間,1、時(shí)間綴;2、時(shí)間字符串
     @return ret 得出的時(shí)間,格式為:2018-01-01 00:00:00
     */
    this.dateadd = function(dur,time = false){
        var test_year = /(-)?\d+(\s)?year(s)?/.exec(dur)
        var test_month = /(-)?\d+(\s)?month(s)?/.exec(dur)
        var test_day = /(-)?\d+(\s)?day(s)?/.exec(dur)
        var test_hour = /(-)?\d+(\s)?hour(s)?/.exec(dur)
        var test_minute = /(-)?\d+(\s)?minute(s)?/.exec(dur)
        var test_second = /(-)?\d+(\s)?second(s)?/.exec(dur)
        test_year = (!test_year)?0:parseInt(test_year[0])
        test_month = (!test_month)?0:parseInt(test_month[0])
        test_day = (!test_day)?0:parseInt(test_day[0])
        test_hour = (!test_hour)?0:parseInt(test_hour[0])
        test_minute = (!test_minute)?0:parseInt(test_minute[0])
        test_second = (!test_second)?0:parseInt(test_second[0])
        var outtime
        if(time){
            if(((typeof(time)=='string') || (time instanceof String)) && (/^\d{4}(-\d{2}(-\d{2})?)?(\s)?(\d{2})?(:)?(\d{2})?(:)?(\d{2})?$/.test(time))){
                outtime = new Date(time)
                outtime.setFullYear(parseInt(outtime.getFullYear()) + test_year)
                outtime.setMonth(parseInt(outtime.getMonth()) + test_month)
                outtime.setDate(parseInt(outtime.getDate()) + test_day)
                outtime.setHours(parseInt(outtime.getHours()) + test_hour)
                outtime.setMinutes(parseInt(outtime.getMinutes()) + test_minute)
                outtime.setSeconds(parseInt(outtime.getSeconds()) + test_second)
                var month = this.parsetime(parseInt(outtime.getUTCMonth())+1)
                var date = this.parsetime(parseInt(outtime.getUTCDate()))
                var hour = this.parsetime(parseInt(outtime.getUTCHours()))
                var minute = this.parsetime(parseInt(outtime.getUTCMinutes()))
                var second = this.parsetime(parseInt(outtime.getUTCSeconds()))
            }else{
                outtime = new Date(time*1000)
                outtime.setFullYear(parseInt(outtime.getFullYear()) + test_year)
                outtime.setMonth(parseInt(outtime.getMonth()) + test_month)
                outtime.setDate(parseInt(outtime.getDate()) + test_day)
                outtime.setHours(parseInt(outtime.getHours()) + test_hour)
                outtime.setMinutes(parseInt(outtime.getMinutes()) + test_minute)
                outtime.setSeconds(parseInt(outtime.getSeconds()) + test_second)
                var month = this.parsetime(parseInt(outtime.getMonth())+1)
                var date = this.parsetime(parseInt(outtime.getDate()))
                var hour = this.parsetime(parseInt(outtime.getHours()))
                var minute = this.parsetime(parseInt(outtime.getMinutes()))
                var second = this.parsetime(parseInt(outtime.getSeconds()))
            }
        }else{
            outtime = new Date()
            outtime.setFullYear(parseInt(outtime.getFullYear()) + test_year)
            outtime.setMonth(parseInt(outtime.getMonth()) + test_month)
            outtime.setDate(parseInt(outtime.getDate()) + test_day)
            outtime.setHours(parseInt(outtime.getHours()) + test_hour)
            outtime.setMinutes(parseInt(outtime.getMinutes()) + test_minute)
            outtime.setSeconds(parseInt(outtime.getSeconds()) + test_second)
            var month = this.parsetime(parseInt(outtime.getMonth())+1)
            var date = this.parsetime(parseInt(outtime.getDate()))
            var hour = this.parsetime(parseInt(outtime.getHours()))
            var minute = this.parsetime(parseInt(outtime.getMinutes()))
            var second = this.parsetime(parseInt(outtime.getSeconds()))
        }
        var year = outtime.getFullYear()
        var ret = year+'-'+month+'-'+date+' '+hour+':'+minute+':'+second
        return ret
    }
    /*
     @desc:求出當(dāng)年/月/日的時(shí)間
     @param time 傳入時(shí)間,格式為:2018|2018-01|2018-01-01
     @return ret stime:開(kāi)始時(shí)間綴,etime:結(jié)束時(shí)間綴
     */
    this.timeswitch = function(time){
        if(/^\d{4}$/.test(time)){  // 年
            var stime = this.strtotime(time)
            var etime = this.strtotime(this.dateadd('+1year',time))
        }else if(/^\d{4}-{1}\d{1,2}$/.test(time)){  // 月
            var stime = this.strtotime(time)
            var etime = this.strtotime(this.dateadd('+1month',time))
        }else if(/^\d{4}-{1}\d{1,2}-{1}\d{1,2}$/.test(time)){  // 日
            var stime = this.strtotime(time)
            var etime = this.strtotime(this.dateadd('+1day',time))
        }else{  // 格式錯(cuò)誤
            return false;
        }
        var ret = {
            stime:stime,
            etime:etime
        }
        return ret
    }
    /*
     @desc:時(shí)間友好顯示函數(shù)
     @param stamp 時(shí)間綴,10位
     @return ret 轉(zhuǎn)換后的時(shí)間顯示
     */
    this.timefriend = function(stamp){
        if(!stamp){
            return '—';
        }
        var date = new Date(stamp*1000)
        var time = Math.round(new Date().getTime()/1000)
        diff = time - stamp
        if(diff<0){
            return '—';
        }else if(diff<60){
            return diff+'秒前'
        }else if(diff<3600){
            return Math.floor(diff/60)+'分鐘前'
        }else if(diff<86400){
            return Math.floor(diff/3600)+'小時(shí)前'
        }else if(diff<259200){
            return Math.floor(diff/86400)+'天前'
        }else{
            var year = this.parsetime(date.getFullYear())
            var month = this.parsetime(date.getMonth()+1)
            var day = this.parsetime(date.getDate())
            var hour = this.parsetime(date.getHours())
            var minute = this.parsetime(date.getMinutes())
            var second = this.parsetime(date.getSeconds())
            var ret = year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second
            return ret
        }
    }
    }
  2. 測(cè)試:
    var datetime = new datetime()
    var ret1 = datetime.timetostr(1528593779)
    var ret2 = datetime.strtotime('2018')
    var ret3 = datetime.timediff('2017','2018-06')
    var ret4 = datetime.dateadd('-1year','2018')
    var ret5 = datetime.timeswitch('2018-06')
    var ret6 = datetime.timefriend(1528593779)
    console.log(ret1)
    console.log(ret2)
    console.log(ret3)
    console.log(ret4)
    console.log(ret5)
    console.log(ret6)
  3. 輸出:
    2018-06-10 09:22:59
    1514736000
    44582400
    2017-01-01 00:00:00
    { stime: '1527782400', etime: '1530374400' }
    1天前
向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI