javascript格式化时间输出
2011-06-16 09:23
429 查看
使用方法如: new Date().formatLocale("yyyy-mm-dd HH:MM:ss", false, "CN"); 即可以输出 2011-05-20 16:42:00
具体使用方法详见:http://blog.stevenlevithan.com/archives/date-time-format。推荐大家在项目中使用。
附代码如下:
具体使用方法详见:http://blog.stevenlevithan.com/archives/date-time-format。推荐大家在项目中使用。
附代码如下:
var dateFormat = function () { var token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])/1?|[LloSZ]|"[^"]*"|'[^']*'/g, timezone = //b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]/d{4})?)/b/g, timezoneClip = /[^-+/dA-Z]/g, pad = function (val, len) { val = String(val); len = len || 2; while (val.length < len) val = "0" + val; return val; }; // Regexes and supporting functions are cached through closure return function (date, mask, utc, locale) { var dF = dateFormat; // You can't provide utc if you skip other args (use the "UTC:" mask prefix) if (arguments.length == 1 && Object.prototype.toString.call(date) == "[object String]" && !//d/.test(date)) { mask = date; date = undefined; } // Passing date through Date applies Date.parse, if necessary date = date ? new Date(date) : new Date; if (isNaN(date)) throw SyntaxError("invalid date"); mask = String(dF.masks[mask] || mask || dF.masks["default"]); // Allow setting the utc argument via the mask if (mask.slice(0, 4) == "UTC:") { mask = mask.slice(4); utc = true; } locale = locale ? ((dateFormat.i18n[locale])? locale : "CN") : "CN"; var _ = utc ? "getUTC" : "get", d = date[_ + "Date"](), D = date[_ + "Day"](), m = date[_ + "Month"](), y = date[_ + "FullYear"](), H = date[_ + "Hours"](), M = date[_ + "Minutes"](), s = date[_ + "Seconds"](), L = date[_ + "Milliseconds"](), o = utc ? 0 : date.getTimezoneOffset(), flags = { d: d, dd: pad(d), ddd: dF.i18n[locale].dayNames[D], dddd: dF.i18n[locale].dayNames[D + 7], m: m + 1, mm: pad(m + 1), mmm: dF.i18n[locale].monthNames[m], mmmm: dF.i18n[locale].monthNames[m + 12], yy: String(y).slice(2), yyyy: y, h: H % 12 || 12, hh: pad(H % 12 || 12), H: H, HH: pad(H), M: M, MM: pad(M), s: s, ss: pad(s), l: pad(L, 3), L: pad(L > 99 ? Math.round(L / 10) : L), t: H < 12 ? "a" : "p", tt: H < 12 ? "am" : "pm", T: H < 12 ? "A" : "P", TT: H < 12 ? "AM" : "PM", Z: utc ? "UTC" : (String(date).match(timezone) || [""]).pop().replace(timezoneClip, ""), o: (o > 0 ? "-" : "+") + pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4), S: ["th", "st", "nd", "rd"][d % 10 > 3 ? 0 : (d % 100 - d % 10 != 10) * d % 10] }; return mask.replace(token, function ($0) { return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1); }); }; }(); // Some common format strings dateFormat.masks = { "default": "ddd mmm dd yyyy HH:MM:ss", shortDate: "m/d/yy", mediumDate: "mmm d, yyyy", longDate: "mmmm d, yyyy", fullDate: "dddd, mmmm d, yyyy", shortTime: "h:MM TT", mediumTime: "h:MM:ss TT", longTime: "h:MM:ss TT Z", isoDate: "yyyy-mm-dd", isoTime: "HH:MM:ss", isoDateTime: "yyyy-mm-dd'T'HH:MM:ss", isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'" }; //不同国家地区对应UTC时差 dateFormat.locale = { "CN":{"name":"中国","language":"zh","offset":0}, "HK":{"name":"中国(香港)","language":"zh","offset":0}, "TW":{"name":"中国(台湾)","language":"zh","offset":0}, "GB":{"name":"英国","language":"en","offset":0}, "US":{"name":"美国","language":"en","offset":0} } // Internationalization strings dateFormat.i18n = { "GB":{ "dayNames": [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ], "monthNames": [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ] }, "US":{ "dayNames": [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ], "monthNames": [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ] }, "CN":{ "dayNames": [ "日", "一", "二", "三", "四", "五", "六", "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" ], "monthNames": [ "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二", "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月" ] }, "HK":{ "dayNames": [ "日", "一", "二", "三", "四", "五", "六", "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" ], "monthNames": [ "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二", "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月" ] }, "TW":{ "dayNames": [ "日", "一", "二", "三", "四", "五", "六", "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" ], "monthNames": [ "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二", "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月" ] } }; // For convenience... Date.prototype.formatLocale = function (mask, utc, locale) { return dateFormat(this, mask, utc, locale); }; Date.prototype.format = function(fmt, locale, utc) { var o = { "M+": this.getMonth() + 1, // 月份 "d+": this.getDate(), // 日 "h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, // 小时 "H+": this.getHours(), // 小时 "m+": this.getMinutes(), // 分 "s+": this.getSeconds(), // 秒 "q+": Math.floor((this.getMonth() + 3) / 3), // 季度 "S": this.getMilliseconds() // 毫秒 }; var week = { "0": "/日", "1": "/一", "2": "/二", "3": "/三", "4": "/四", "5": "/五", "6": "/六" }; if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); } if (/(E+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "/星/期": "/周") : "") + week[this.getDay() + ""]); } for (var k in o) { if (new RegExp("(" + k + ")").test(fmt)) { fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); } } return fmt; } /** * 下一年 */ Date.prototype.nextyear = function() { return new Date(this.getFullYear()+1, this.getMonth(), this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds(), this.getMilliseconds()); }
相关文章推荐
- javascript格式化输出时间
- javascript扩展date对象格式化日期时间输出
- javascript扩展date对象格式化日期时间输出(二)
- JavaScript中Date对象与格式化时间输出
- JavaScript格式化日期时间的方法和自定义格式化函数示例
- js将当前时间日期格式化输出
- javascript 格式化时间日期函数代码脚本之家修正版
- C语言格式化输出时间函数strftime()
- PHP中格式化输出当前时间
- javascript:时间格式化
- JavaScript将时间格式化为yyyy-MM-dd hh:mm:ss
- javascript格式化日期时间方法汇总
- Asp.net 中将日期时间值,转换成字符串,格式化输出
- 在<s:iterator>中,将时间输出显示格式化
- python中日期和时间格式化输出
- [JavaScript] 时间戳格式化为yyyy-MM-dd日期
- MFC显示时间,日期,格式化输出
- javascript格式化时间
- fortran-时间的格式化输出
- js 获取时间 格式化输出