最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript日期時(shí)間與時(shí)間戳的轉(zhuǎn)換函數(shù)分享

 更新時(shí)間:2015年01月31日 11:08:50   投稿:junjie  
這篇文章主要介紹了JavaScript日期時(shí)間與時(shí)間戳的轉(zhuǎn)換函數(shù)分享,本文給出兩個(gè)函數(shù)實(shí)現(xiàn)日期時(shí)間和時(shí)間戳間的轉(zhuǎn)換,需要的朋友可以參考下

如果只是將當(dāng)前時(shí)間轉(zhuǎn)成時(shí)間戳,可以直接使用new Date().getTime()/1000;但如果是將某個(gè)具體時(shí)間或日期轉(zhuǎn)成Unix時(shí)間戳,ie不支持像new Date(“2013-1-1”)  這樣帶參數(shù)的方法,將返回NaN。

對(duì)此,我寫了以下函數(shù),支持ie6+,谷歌,火狐等瀏覽器:

復(fù)制代碼 代碼如下:

function getTime(day){
 re = /(\d{4})(?:-(\d{1,2})(?:-(\d{1,2}))?)?(?:\s+(\d{1,2}):(\d{1,2}):(\d{1,2}))?/.exec(day);
 return new Date(re[1],(re[2]||1)-1,re[3]||1,re[4]||0,re[5]||0,re[6]||0).getTime()/1000;
}

//test
alert(getTime("2013-02-03 10:10:10"));
alert(getTime("2013-02-03"));
alert(getTime("2013-02"));
alert(getTime("2013"));

下面這個(gè)將時(shí)間戳轉(zhuǎn)換成日期格式的函數(shù),支持自定義的日期格式,效果類似PHP的date函數(shù),同樣支持ie6+,谷歌,火狐等瀏覽器。這個(gè)函數(shù)是網(wǎng)友實(shí)現(xiàn)的,以后有時(shí)間我也寫一個(gè)出來 ^_^

復(fù)制代碼 代碼如下:

function date(format, timestamp){
    var a, jsdate=((timestamp) ? new Date(timestamp*1000) : new Date());
    var pad = function(n, c){
        if((n = n + "").length < c){
            return new Array(++c - n.length).join("0") + n;
        } else {
            return n;
        }
    };
    var txt_weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
    var txt_ordin = {1:"st", 2:"nd", 3:"rd", 21:"st", 22:"nd", 23:"rd", 31:"st"};
    var txt_months = ["", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
    var f = {
        // Day
        d: function(){return pad(f.j(), 2)},
        D: function(){return f.l().substr(0,3)},
        j: function(){return jsdate.getDate()},
        l: function(){return txt_weekdays[f.w()]},
        N: function(){return f.w() + 1},
        S: function(){return txt_ordin[f.j()] ? txt_ordin[f.j()] : 'th'},
        w: function(){return jsdate.getDay()},
        z: function(){return (jsdate - new Date(jsdate.getFullYear() + "/1/1")) / 864e5 >> 0},
     
        // Week
        W: function(){
            var a = f.z(), b = 364 + f.L() - a;
            var nd2, nd = (new Date(jsdate.getFullYear() + "/1/1").getDay() || 7) - 1;
            if(b <= 2 && ((jsdate.getDay() || 7) - 1) <= 2 - b){
                return 1;
            } else{
                if(a <= 2 && nd >= 4 && a >= (6 - nd)){
                    nd2 = new Date(jsdate.getFullYear() - 1 + "/12/31");
                    return date("W", Math.round(nd2.getTime()/1000));
                } else{
                    return (1 + (nd <= 3 ? ((a + nd) / 7) : (a - (7 - nd)) / 7) >> 0);
                }
            }
        },
     
        // Month
        F: function(){return txt_months[f.n()]},
        m: function(){return pad(f.n(), 2)},
        M: function(){return f.F().substr(0,3)},
        n: function(){return jsdate.getMonth() + 1},
        t: function(){
            var n;
            if( (n = jsdate.getMonth() + 1) == 2 ){
                return 28 + f.L();
            } else{
                if( n & 1 && n < 8 || !(n & 1) && n > 7 ){
                    return 31;
                } else{
                    return 30;
                }
            }
        },
     
        // Year
        L: function(){var y = f.Y();return (!(y & 3) && (y % 1e2 || !(y % 4e2))) ? 1 : 0},
        //o not supported yet
        Y: function(){return jsdate.getFullYear()},
        y: function(){return (jsdate.getFullYear() + "").slice(2)},
     
        // Time
        a: function(){return jsdate.getHours() > 11 ? "pm" : "am"},
        A: function(){return f.a().toUpperCase()},
        B: function(){
            // peter paul koch:
            var off = (jsdate.getTimezoneOffset() + 60)*60;
            var theSeconds = (jsdate.getHours() * 3600) + (jsdate.getMinutes() * 60) + jsdate.getSeconds() + off;
            var beat = Math.floor(theSeconds/86.4);
            if (beat > 1000) beat -= 1000;
            if (beat < 0) beat += 1000;
            if ((String(beat)).length == 1) beat = "00"+beat;
            if ((String(beat)).length == 2) beat = "0"+beat;
            return beat;
        },
        g: function(){return jsdate.getHours() % 12 || 12},
        G: function(){return jsdate.getHours()},
        h: function(){return pad(f.g(), 2)},
        H: function(){return pad(jsdate.getHours(), 2)},
        i: function(){return pad(jsdate.getMinutes(), 2)},
        s: function(){return pad(jsdate.getSeconds(), 2)},
        //u not supported yet
     
        // Timezone
        //e not supported yet
        //I not supported yet
        O: function(){
            var t = pad(Math.abs(jsdate.getTimezoneOffset()/60*100), 4);
            if (jsdate.getTimezoneOffset() > 0) t = "-" + t; else t = "+" + t;
            return t;
        },
        P: function(){var O = f.O();return (O.substr(0, 3) + ":" + O.substr(3, 2))},
        //T not supported yet
        //Z not supported yet
     
        // Full Date/Time
        c: function(){return f.Y() + "-" + f.m() + "-" + f.d() + "T" + f.h() + ":" + f.i() + ":" + f.s() + f.P()},
        //r not supported yet
        U: function(){return Math.round(jsdate.getTime()/1000)}
    };
     
    return format.replace(/[\\]?([a-zA-Z])/g, function(t, s){
        if( t!=s ){
            // escaped
            ret = s;
        } else if( f[s] ){
            // a date function exists
            ret = f[s]();
        } else{
            // nothing special
            ret = s;
        }
        return ret;
    });
}

//test
alert(date('Y-m-d H:i:s',(new Date).getTime()/1000));
alert(date('Y-m-d',(new Date).getTime()/1000));
alert(date('Y-m-d H:i:s','1355252653'));

相關(guān)文章

  • TypeScript中函數(shù)重載寫法

    TypeScript中函數(shù)重載寫法

    這篇文章主要介紹了TypeScript中函數(shù)重載寫法,TypeScript 提供了函數(shù)重載功能,下面文章圍繞TypeScript函數(shù)重載續(xù)航管資料展開內(nèi)容,具有一定得參考價(jià)值,需要的朋友可以參考一下
    2021-12-12
  • 解決uni-app微信小程序input輸入框在底部時(shí),鍵盤彈起頁(yè)面整體上移問題

    解決uni-app微信小程序input輸入框在底部時(shí),鍵盤彈起頁(yè)面整體上移問題

    問題是這樣的input?獲取焦點(diǎn)時(shí)會(huì)自動(dòng)調(diào)起手機(jī)鍵盤,設(shè)置?:adjust-position="true",會(huì)導(dǎo)致鍵盤彈起時(shí)頁(yè)面整體上移,這篇文章主要介紹了解決uni-app微信小程序input輸入框在底部時(shí),鍵盤彈起頁(yè)面整體上移問題,,需要的朋友可以參考下
    2022-08-08
  • 20道JS原理題助你面試一臂之力(必看)

    20道JS原理題助你面試一臂之力(必看)

    本文針對(duì)目前常見的面試題,僅提供了相應(yīng)的核心原理及思路,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-07-07
  • Javascript中call,apply,bind方法的詳解與總結(jié)

    Javascript中call,apply,bind方法的詳解與總結(jié)

    本文主要Javascript中call,apply,bind方法的進(jìn)行全面分析,并在文章結(jié)尾對(duì)call,apply,bind方法的聯(lián)系和區(qū)別做了總結(jié),具有很好的參考價(jià)值,需要的朋友一起來看下吧
    2016-12-12
  • echarts大屏字體自適應(yīng)的方法步驟

    echarts大屏字體自適應(yīng)的方法步驟

    這篇文章主要介紹了echarts大屏字體自適應(yīng)的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • javascript動(dòng)態(tài)加載三

    javascript動(dòng)態(tài)加載三

    首先是通過同步策略來實(shí)現(xiàn)模塊加載與回調(diào)函數(shù)之間進(jìn)行分離,接著是通過異步策略來實(shí)現(xiàn)模塊加載與回調(diào)函數(shù)之間進(jìn)行分離
    2012-08-08
  • JavaScript計(jì)算字符串中特定字符出現(xiàn)次數(shù)的實(shí)例詳解

    JavaScript計(jì)算字符串中特定字符出現(xiàn)次數(shù)的實(shí)例詳解

    在JavaScript編程中,經(jīng)常會(huì)遇到需要計(jì)算字符串中特定字符出現(xiàn)次數(shù)的情況,在本文中,我將分享兩個(gè)簡(jiǎn)單的JavaScript函數(shù),用于計(jì)算字符串中特定字符出現(xiàn)的次數(shù),需要的朋友可以參考下
    2023-11-11
  • JavaScript實(shí)現(xiàn)微信號(hào)隨機(jī)切換代碼

    JavaScript實(shí)現(xiàn)微信號(hào)隨機(jī)切換代碼

    這篇文章主要介紹了JavaScript實(shí)現(xiàn)微信號(hào)隨機(jī)切換代碼,需要的朋友可以參考下
    2018-03-03
  • JS實(shí)現(xiàn)電商商品展示放大鏡特效

    JS實(shí)現(xiàn)電商商品展示放大鏡特效

    這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)電商商品展示放大鏡特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-01-01
  • js獲取瀏覽器的可視區(qū)域尺寸的實(shí)現(xiàn)代碼

    js獲取瀏覽器的可視區(qū)域尺寸的實(shí)現(xiàn)代碼

    js獲取瀏覽器的可視區(qū)域尺寸的實(shí)現(xiàn)代碼,需要的朋友可以參考下。
    2011-11-11

最新評(píng)論

金溪县| 迁西县| 平顶山市| 通道| 富民县| 高尔夫| 宜春市| 临江市| 武城县| 新兴县| 嵩明县| 秦安县| 合肥市| 蓬安县| 曲靖市| 纳雍县| 米易县| 荔波县| 惠来县| 德阳市| 通许县| 常德市| 将乐县| 正蓝旗| 峨山| 沧州市| 阳高县| 敦煌市| 环江| 宽甸| 尼勒克县| 安新县| 弥勒县| 遵义县| 红原县| 铜陵市| 金华市| 安国市| 广汉市| 深州市| 崇州市|