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

JS日期格式化之javascript Date format_javascript技巧

在上篇文章给大家介绍了js对date对象的操作的问题(生成一个倒数7天的数组),本篇介绍有关js日期格式化之javascript date format,本文通过三种方法给大家讲解,具体内容请看下文。
方法一:
// 对date的扩展,将 date 转化为指定格式的string// 月(m)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占位符,毫秒(s)只能用 1 个占位符(是 1-3 位的数字) // 例子: // (new date()).format(yyyy-mm-dd hh:mm:ss.s) ==> 2006-07-02 08:09:04.423 // (new date()).format(yyyy-m-d h:m:s.s) ==> 2006-7-2 8:9:4.18 date.prototype.format = function (fmt) { //author: meizz var o = { m+: this.getmonth() + 1, //月份 d+: this.getdate(), //日 h+: this.gethours(), //小时 m+: this.getminutes(), //分 s+: this.getseconds(), //秒 q+: math.floor((this.getmonth() + 3) / 3), //季度 s: this.getmilliseconds() //毫秒 }; if (/(y+)/.test(fmt)) fmt = fmt.replace(regexp.$1, (this.getfullyear() + ).substr(4 - regexp.$1.length)); 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;}
调用方式:
var time1 = new date().format(yyyy-mm-dd);var time2 = new date().format(yyyy-mm-dd hh:mm:ss);
方法二:

方法三:
date.prototype.format = function (mask) { var d = this; var zeroize = function (value, length) { if (!length) length = 2; value = string(value); for (var i = 0, zeros = ''; i 99) m = math.round(m / 10); return zeroize(m); case 'tt': return d.gethours() < 12 ? 'am' : 'pm'; case 'tt': return d.gethours() < 12 ? 'am' : 'pm'; case 'z': return d.toutcstring().match(/[a-z]+$/); // return quoted strings with the surrounding quotes removed default: return $0.substr(1, $0.length - 2); } });};
以上内容是本文给大家介绍的js日期格式化之javascript date format,希望大家喜欢。
其它类似信息

推荐信息