javascript強大的日期函數(shù)代碼分享
更新時間:2013年09月04日 16:28:44 作者:
這篇文章介紹了javascript強大的日期函數(shù)代碼,有需要的朋友可以參考一下
復制代碼 代碼如下:
var date = function( a, s ) {
var d = s ? new Date( s ) : new Date(), f = d.getTime();
return ( '' + a ).replace( /a|A|d|D|F|g|G|h|H|i|I|j|l|L|m|M|n|s|S|t|T|U|w|y|Y|z|Z/g, function ( a ) {
switch ( a ) {
case 'a' : return d.getHours() > 11 ? 'pm' : 'am';
case 'A' : return d.getHours() > 11 ? 'PM' : 'AM';
case 'd' : return ( '0' + d.getDate() ).slice(-2);
case 'D' : return [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ][ d.getDay() ];
case 'F' : return [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ][ d.getMonth() ];
case 'g' : return ( s = ( d.getHours() || 12 ) ) > 12 ? s - 12 : s;
case 'G' : return d.getHours();
case 'h' : return ( '0' + ( ( s = d.getHours() || 12 ) > 12 ? s - 12 : s ) ).slice(-2);
case 'H' : return ( '0' + d.getHours() ).slice(-2);
case 'i' : return ( '0' + d.getMinutes() ).slice(-2);
case 'I' : return (function(){ d.setDate(1); d.setMonth(0); s = [ d.getTimezoneOffset() ]; d.setMonth(6); s[1] = d.getTimezoneOffset(); d.setTime( f ); return s[0] == s[1] ? 0 : 1; })();
case 'j' : return d.getDate();
case 'l' : return [ 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ][ d.getDay() ];
case 'L' : return ( s = d.getFullYear() ) % 4 == 0 && ( s % 100 != 0 || s % 400 == 0 ) ? 1 : 0;
case 'm' : return ( '0' + ( d.getMonth() + 1 ) ).slice(-2);
case 'M' : return [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ][ d.getMonth() ];
case 'n' : return d.getMonth() + 1;
case 's' : return ( '0' + d.getSeconds() ).slice(-2);
case 'S' : return [ 'th', 'st', 'nd', 'rd' ][ ( s = d.getDate() ) < 4 ? s : 0 ];
case 't' : return (function(){ d.setDate(32); s = 32 - d.getDate(); d.setTime( f ); return s; })();
case 'T' : return 'UTC';
case 'U' : return ( '' + f ).slice( 0, -3 );
case 'w' : return d.getDay();
case 'y' : return ( '' + d.getFullYear() ).slice(-2);
case 'Y' : return d.getFullYear();
case 'z' : return (function(){ d.setMonth(0); return d.setTime( f - d.setDate(1) ) / 86400000; })();
default : return -d.getTimezoneOffset() * 60;
};
} );
};
您可能感興趣的文章:
- 巧用批處理解決IE不支持JavaScript等問題
- javascript 格式化時間日期函數(shù)代碼腳本之家修正版
- JavaScript性能優(yōu)化 創(chuàng)建文檔碎片(document.createDocumentFragment)
- 自己整理的一個javascript日期處理函數(shù)
- JavaScript(JS) 壓縮 / 混淆 / 格式化 批處理工具
- 高性能Javascript筆記 數(shù)據(jù)的存儲與訪問性能優(yōu)化
- JavaScript中的無阻塞加載性能優(yōu)化方案
- javascript中日期函數(shù)new Date()的瀏覽器兼容性問題
- javascript日期處理函數(shù),性能優(yōu)化批處理
相關文章
JavaScript中null和undefined的區(qū)別詳解
null 是一個原始值,表示“無”或“空值”,它通常用于指示變量應有的對象或值不存在,undefined 是一個原始值,表示“未定義”,本文給大家詳細介紹了JavaScript中null和undefined的區(qū)別,需要的朋友可以參考下2024-10-10
CKEditor 4.4.1 添加代碼高亮顯示插件功能教程【使用官方推薦Code Snippet插件】
這篇文章主要介紹了CKEditor 4.4.1 添加代碼高亮顯示插件功能,涉及ckeditor使用官方推薦Code Snippet插件的相關操作布局與使用注意事項,需要的朋友可以參考下2019-06-06
Add Formatted Text to a Word Document
Add Formatted Text to a Word Document...2007-06-06

