jquery教程

推荐列表 站点导航

当前位置:首页 > jquery > jquery教程 >

javascript 格式化时间日期函数代码

来源:网络整理  作者:wy  发布时间:2020-12-23 03:26
分享一个javascript 格式化时间日期函数,修正后的js日期格式化函数,有需要的朋友参考下。...

javascript中new Date()得到的是一个国际化时间格式的时间值,在使用中文时不方便。
需要对javascript中的日期时间进行格式化。

来看下面的例子:
 

复制代码 代码示例:

<script>
Date.prototype.format = function(format)
{
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
} //js格式化
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;
}
//调用的方法
var t=new Date();
document.write("格式化前:"+t+"<br>");
t=new Date().format("yyyy-MM-dd hh:mm:ss");
document.write("格式化后:"+t);
</script>
 

js时间戳格式化成日期格式的几种方法
JS时间格式化的方法汇总
javascript实现的Date日期格式化函数
javascript实现(Date format)日期格式化的三个例子
javascript 格式化货币数据的代码
JS Number对象格式化方法的实例代码
js 日期格式化的例子
javascript格式化时间日期函数举例
js格式化日期时间型数据函数的实现代码
通过正则格式化url查询字符串的代码

相关热词:

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!

本文地址: https://v30.fanwenzhu.com/jq/jc/7876.shtml

最新文章
PHP识别相片是否是颠倒的 PHP识别相片是否是颠倒的

时间:2020-12-28

python编程有哪些ide python编程有哪些ide

时间:2020-12-28

python开发工程师是做什么 python开发工程师是做什么

时间:2020-12-28

php构造函数的作用 php构造函数的作用

时间:2020-12-28

php怎么跟数据库连接 php怎么跟数据库连接

时间:2020-12-28

php实现顺序线性表 php实现顺序线性表

时间:2020-12-28

Python多重继承中的菱形继 Python多重继承中的菱形继

时间:2020-12-28

php中break的作用 php中break的作用

时间:2020-12-28

Copyright © www.juheyunku.com      关于 | 合作 | 声明 | 联系 | 更新 | 地图 | Tags

javascript 格式化时间日期函数代码

2020-12-23 编辑:wy

javascript中new Date()得到的是一个国际化时间格式的时间值,在使用中文时不方便。
需要对javascript中的日期时间进行格式化。

来看下面的例子:
 

复制代码 代码示例:

<script>
Date.prototype.format = function(format)
{
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
} //js格式化
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;
}
//调用的方法
var t=new Date();
document.write("格式化前:"+t+"<br>");
t=new Date().format("yyyy-MM-dd hh:mm:ss");
document.write("格式化后:"+t);
</script>
 

js时间戳格式化成日期格式的几种方法
JS时间格式化的方法汇总
javascript实现的Date日期格式化函数
javascript实现(Date format)日期格式化的三个例子
javascript 格式化货币数据的代码
JS Number对象格式化方法的实例代码
js 日期格式化的例子
javascript格式化时间日期函数举例
js格式化日期时间型数据函数的实现代码
通过正则格式化url查询字符串的代码

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供学习参考!
本文地址为 https://v30.fanwenzhu.com/jq/jc/7876.shtml

相关文章

风云图片

推荐阅读

返回jquery教程频道首页