您好,欢迎访问一九零五行业门户网

和PHP的date函数用法一样的JS时间戳格式化函数

/**
 * 和php一样的时间戳格式化函数
 * @param  {string} format    格式
 * @param  {int}    timestamp 要格式化的时间 默认为当前时间
 * @return {string}           格式化的时间字符串
 */
function date(format, timestamp){ 
    var a, jsdate=((timestamp) ? new date(timestamp*1000) : new date());
    var pad = function(n, c){
        if((n = n + ).length             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                 return 1;
            } else{
                if(a = 4 && a >= (6 - nd)){
                    nd2 = new date(jsdate.getfullyear() - 1 + /12/31);
                    return date(w, math.round(nd2.gettime()/1000));
                } else{
                    return (1 + (nd > 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  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             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;
    });
} ad:真正免费,域名+虚机+企业邮箱=0元
其它类似信息

推荐信息