javascript格式化时间
2010-06-09 10:26
357 查看
<html>
<head>
<script>
/**
* 时间对象的格式化;
*/
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;
}
</script>
<script>
var now = new Date();
var ms = now.getTime();
alert(new Date(ms).format("hh:mm MM-dd-yyyy"));
</script>
</head>
<body>
</body>
</html>
<head>
<script>
/**
* 时间对象的格式化;
*/
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;
}
</script>
<script>
var now = new Date();
var ms = now.getTime();
alert(new Date(ms).format("hh:mm MM-dd-yyyy"));
</script>
</head>
<body>
</body>
</html>
相关文章推荐
- 用JavaScript(js)对时间格式化
- [JavaScript] 时间戳格式化为yyyy-MM-dd日期
- 全面兼容的javascript时间格式化函数
- javascript格式化时间
- javascript时间相加相减格式化
- JavaScript 格式化日期,转换时间日期格式
- JavaScript如何将unix时间戳转化为UTC时间并格式化显示
- javascript格式化输出时间
- javascript:时间格式化
- JavaScript格式化当前时间为yyyy-mm-dd形式
- javascript下Date对象的时间格式化函数
- javascript 格式化时间日期函数代码脚本之家修正版
- javascript扩展date对象格式化日期时间输出
- 【JavaScript】时间戳格式化
- 全面兼容的javascript时间格式化函数(比较实用)
- javascript中的时间格式化处理
- JavaScript时间戳与其格式化
- JavaScript 格式化时间日期
- javascript格式化时间
- Javascript 日期时间格式化