//testalert(date(‘Y-m-d
H:i:s’,/1000));alert(date.getTime;alert(date(‘Y-m-d
H:i:s’,’1355252653′));

        // 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

return format.replace/g, function{ // escaped ret = s; } else if{ // a
date function exists ret = f[s](); } else{ // nothing special ret = s;
}

复制代码 代码如下:function date{ var a,
jsdate= ? new Date : new Date; var pad = function{ if.length < c卡塔尔(قطر‎{
return new Array.join + 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, 2)}, D: function.substr{return jsdate.getDate{return
txt_weekdays[f.w{return f.w{return txt_ordin[f.j()] ?
txt_ordin[f.j()] : ‘th’}, w: function(){return jsdate.getDay{return
(jsdate – new Date + “/1/1”)) / 864e5 >> 0}, // Week W: function,
b = 364 + f.L() – a; var nd2, nd = (new Date + “/1/1”).getDay – 1; if(b
<= 2 && || 7) – 1) <= 2 – b){ return 1; } else{ if(a <= 2 && nd
>= 4 && a >= { nd2 = new Date – 1 + “/12/31”); return date(“W”,
Math.round; } else{ return (1 + (nd <= 3 ? : >> 0); } } }, //
Month F: function(){return txt_months[f.n{return pad}, M:
function.substr{return jsdate.getMonth{ var n; if( + 1) == 2 ){ return
28 + f.L(); } else{ if( n & 1 && n < 8 || !{ return 31; } else{
return 30; } } }, // Year L: function;return && )) ? 1 : 0}, //o not
supported yet Y: function(){return jsdate.getFullYear{return +
“”).slice}, // Time a: function(){return jsdate.getHours() > 11 ?
“pm” : “am”}, A: function.toUpperCase{ // peter paul koch: var off =
(jsdate.getTimezoneOffset*60; var theSeconds = * 3600) + * 60) +
jsdate.getSeconds() + off; var beat = Math.floor; if beat -= 1000; if
beat += 1000; if .length == 1) beat = “00”+beat; if .length == 2) beat =
“0”+beat; return beat; }, g: function(){return jsdate.getHours() % 12 ||
12}, G: function(){return jsdate.getHours{return pad}, H:
function(){return pad}, i: function(){return pad, 2)}, s:
function(){return pad, 2)}, //u not supported yet // Timezone //e not
supported yet //I not supported yet O: function(){ var t =
pad(Math.abs(jsdate.getTimezoneOffset; if (jsdate.getTimezoneOffset t =
“-” + t; else t = “+” + t; return t; }, P: function;return + “:” +
O.substr}, //T not supported yet //Z not supported yet // Full Date/Time
c: function + “-” + f.m + “T” + f.h + “:” + f.s}, //r not supported yet
U: function(){return Math.round/1000)} }; return format.replace/g,
function{ // escaped ret = s; } else if{ // a date function exists ret =
f[s](); } else{ // nothing special ret = s; } return ret; });}

    var txt_months = [“”, “January”, “February”, “March”, “April”,
“May”, “June”, “July”, “August”, “September”, “October”, “November”,
“December”];

if(a <= 2 && nd >= 4 && a >= { nd2 = new Date – 1 + “/12/31”);
return date(“W”, Math.round; } else{ return (1 + / 7) : >> 0); } }
},

复制代码 代码如下:function
get提姆e?卡塔尔(英语:State of Qatar)?:?/.exec;return new
Date-1,re[3]||1,re[4]||0,re[5]||0,re[6]||0).getTime()/1000;}

        // 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);
            },

// Full Date/Time c: function + “-” + f.m + “T” + f.h + “:” + f.s; },
//r not supported yet U: function(){ return Math.round/1000); } };

对此,笔者写了以下函数,扶植ie6+,谷歌(Google卡塔尔国,火狐等浏览器:

// 和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 < c ) {
            return new Array(++c – n.length).join(“0”) + n;
        } else {
            return n;
        }
    };

复制代码 代码如下://
和PHP雷同的年月戳格式化函数// @param {string} format 格式// @param {int}
timestamp 要格式化的岁月 默感到当下光阴// @return {string}
格式化的小时字符串function date { var a, jsdate= ? new Date : new Date;
var pad = function{ if.length < c 卡塔尔 { return new Array.join + n; }
else { return n; } };

下边那一个将时刻戳调换到日期格式的函数,协理自定义的日期格式,效果相像PHP的date函数,相仿支撑ie6+,谷歌(Google卡塔尔(قطر‎,火狐等浏览器。那一个函数是网民实现的,今后不常光笔者也写二个出去
^_^

        // 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);
            }
    };

// Timezone //e not supported yet //I not supported yet O: function(){
var t = pad(Math.abs(jsdate.getTimezoneOffset; if
(jsdate.getTimezoneOffset t = “-” + t; else t = “+” + t; return t; }, P:
function; return + “:” + O.substr; }, //T not supported yet //Z not
supported yet

借使只是将近些日子光阴转成时间戳,可以平素运用new
Date/1000;但后生可畏旦是将有个别具体时刻或日期转成Unix时间戳,ie不帮助像new
Date 那样带参数的不二法门,将再次回到NaN。

    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;
        }

var txt_months = [“”, “January”, “February”, “March”, “April”, “May”,
“June”, “July”, “August”, “September”, “October”, “November”,
“December”];

admin

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注