Home > php教程 > php手册 > body text

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

WBOY
Release: 2016-06-07 11:45:45
Original
1376 people have browsed it

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

AD:真正免费,域名+虚机+企业邮箱=0元

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!