展会信息港展会大全

js格式化时间和js格式化时间戳示例
来源:互联网   发布日期:2016-03-24 20:12:02   浏览:1556次  

导读:这篇文章主要介绍了js格式化时间和js格式化时间戳示例,需要的朋友可以参考下代码如下:/*** 时间对象的格式化;*/Date.prototype.format = functi...

这篇文章主要介绍了js格式化时间和js格式化时间戳示例,需要的朋友可以参考下

代码如下:

/**

* 时间对象的格式化;

*/

Date.prototype.format = function(format) {

/*

* eg:format="YYYY-MM-dd hh:mm:ss";

*/

var o = {

"M+" :this.getMonth() + 1, // month

"d+" :this.getDate(), // day

"h+" :this.getHours(), // hour

"m+" :this.getMinutes(), // minute

"s+" :this.getSeconds(), // second

"q+" :Math.floor((this.getMonth() + 3) / 3), // quarter

"S" :this.getMilliseconds()

// millisecond

}

if (/(y+)/.test(format)) {

format = format.replace(RegExp.$1, (this.getFullYear() + "")

.substr(4 - RegExp.$1.length));

}

for ( var k in o) {

if (new RegExp("(" + k + ")").test(format)) {

format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]

: ("00" + o[k]).substr(("" + o[k]).length));

}

}

return format;

}

代码如下:

varnow = new Date().format("yyyy-MM-dd hh:mm:ss");

代码如下:

new Date().format("yy-MM-dd hh:mm");

赞助本站

人工智能实验室

相关热词: 开发 编程 android

相关内容
AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港