当前位置: 移动技术网 > IT编程>开发语言>JavaScript > JavaScript 中Date对象的格式化代码方法汇总

JavaScript 中Date对象的格式化代码方法汇总

2017年12月12日  | 移动技术网IT编程  | 我要评论

 javascript默认的时间格式我们一般情况下不会用,所以需要进行格式化,下面说说我总结的javascript时间格式化方法。

很多时候,我们可以利用javascript中date对象的内置方法来格式化,如:

var d = new date();
console.log(d); // 输出:mon nov 04 2013 21:50:33 gmt+0800 (中国标准时间)
console.log(d.todatestring()); // 日期字符串,输出:mon nov 04 2013
console.log(d.togmtstring()); // 格林威治时间,输出:mon, 04 nov 2013 14:03:05 gmt
console.log(d.toisostring()); // 国际标准组织(iso)格式,输出:2013-11-04t14:03:05.420z
console.log(d.tojson()); // 输出:2013-11-04t14:03:05.420z
console.log(d.tolocaledatestring()); // 转换为本地日期格式,视环境而定,输出:2013年11月4日
console.log(d.tolocalestring()); // 转换为本地日期和时间格式,视环境而定,输出:2013年11月4日 下午10:03:05
console.log(d.tolocaletimestring()); // 转换为本地时间格式,视环境而定,输出:下午10:03:05
console.log(d.tostring()); // 转换为字符串,输出:mon nov 04 2013 22:03:05 gmt+0800 (中国标准时间)
console.log(d.totimestring()); // 转换为时间字符串,输出:22:03:05 gmt+0800 (中国标准时间)
console.log(d.toutcstring()); // 转换为世界时间,输出:mon, 04 nov 2013 14:03:05 gmt

如果上面的方法不能满足我们的要求,也可以自定义函数来格式化时间,如:

 方法一:

// 对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”); 

方法二:

<script language="javascript" type="text/javascript">
<!-- /** * 对date的扩展,将 date 转化为指定格式的string * 月(m)、日(d)、12小时(h)、24小时(h)、分(m)、秒(s)、周(e)、季度(q)
可以用 1-2 个占位符 * 年(y)可以用 1-4 个占位符,毫秒(s)只能用 1 个占位符(是 1-3 位的数字) * eg: * (new
date()).pattern("yyyy-mm-dd hh:mm:ss.s")==> 2006-07-02 08:09:04.423
* (new date()).pattern("yyyy-mm-dd e hh:mm:ss") ==> 2009-03-10 二 20:09:04
* (new date()).pattern("yyyy-mm-dd ee hh:mm:ss") ==> 2009-03-10 周二 08:09:04
* (new date()).pattern("yyyy-mm-dd eee hh:mm:ss") ==> 2009-03-10 星期二 08:09:04
* (new date()).pattern("yyyy-m-d h:m:s.s") ==> 2006-7-2 8:9:4.18
*/
date.prototype.pattern=function(fmt) {
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" : "/u65e5",
"1" : "/u4e00",
"2" : "/u4e8c",
"3" : "/u4e09",
"4" : "/u56db",
"5" : "/u4e94",
"6" : "/u516d"
};
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 ? "/u661f/u671f" : "/u5468") : "")+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;
}
var date = new date();
window.alert(date.pattern("yyyy-mm-dd hh:mm:ss"));
// -->
</script>

方法三:

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 < (length - value.length); i++) {
zeros += '0';
}
return zeros + value;
};
return mask.replace(/"[^"]*"|'[^']*'|/b ( ? : d {
1, 4
} | m {
1, 4
} | yy( ? : yy) ? | ([hhmstt]) / 1 ? | [llz]) / b / g, function ($0) {
switch ($0) {
case 'd':
return d.getdate();
case 'dd':
return zeroize(d.getdate());
case 'ddd':
return ['sun', 'mon', 'tue', 'wed', 'thr', 'fri', 'sat'][d.getday()];
case 'dddd':
return ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'][d.getday()];
case 'm':
return d.getmonth() + 1;
case 'mm':
return zeroize(d.getmonth() + 1);
case 'mmm':
return ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'][d.getmonth()];
case 'mmmm':
return ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december'][d.getmonth()];
case 'yy':
return string(d.getfullyear()).substr(2);
case 'yyyy':
return d.getfullyear();
case 'h':
return d.gethours() % 12 || 12;
case 'hh':
return zeroize(d.gethours() % 12 || 12);
case 'h':
return d.gethours();
case 'hh':
return zeroize(d.gethours());
case 'm':
return d.getminutes();
case 'mm':
return zeroize(d.getminutes());
case 's':
return d.getseconds();
case 'ss':
return zeroize(d.getseconds());
case 'l':
return zeroize(d.getmilliseconds(), 3);
case 'l':
var m = d.getmilliseconds();
if (m > 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);
}
});
};

总结

以上所述是小编给大家介绍的javascript 中date对象的格式化代码方法汇总,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网