Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  javascript  >  正文 js按指定格式显示日期时间的代码

js按指定格式显示日期时间的代码

发布时间:2014-09-30   编辑:www.jquerycn.cn
为大家介绍一例按指定格式显示日期时间的代码,有需要的朋友,可以参考下。

代码如下:

<html>
<head>
<title>按指定格式显示日期与时间-www.jbxue.com</title>
</head>
<body> 
<script> 
/// <summary> 
/// 格式化显示日期时间 
/// </summary> 
/// <param name="x">待显示的日期时间,例如new Date()</param> 
/// <param name="y">需要显示的格式,例如yyyy-MM-dd hh:mm:ss</param> 
function date2str(x,y) { 
var z = {M:x.getMonth()+1,d:x.getDate(),h:x.getHours(),m:x.getMinutes(),s:x.getSeconds()}; 
y = y.replace(/(M+|d+|h+|m+|s+)/g,function(v) {return ((v.length>1?"0":"")+eval('z.'+v.slice(-1))).slice(-2)}); 
return y.replace(/(y+)/g,function(v) {return x.getFullYear().toString().slice(-v.length)}); 
} 
alert(date2str(new Date(),"yyyy-MM-dd hh:mm:ss")); 
alert(date2str(new Date(),"yyyy-M-d h:m:s")); 

//1.2013-4-9 11:21:32
//2.2013年4月9日 11点21分32秒
//3.2013年4月9日 上午11点21分32秒
//4.2013年4月9日 下午13点21分32秒
/*
var thedate = new Date();
alert(thedate.getFullYear() + '-' + thedate.getMonth() + '-' + thedate.getDate() + ' ' + thedate.toLocaleTimeString());
alert(thedate.toLocaleDateString() + ' ' + thedate.getHours() + '点' + thedate.getMinutes() + '分' + thedate.getSeconds() + '秒');
if (thedate.getHours() < 12) {
alert(thedate.toLocaleDateString() + ' 上午' + thedate.getHours() + '点' + thedate.getMinutes() + '分' + thedate.getSeconds() + '秒');
}
else {
alert(thedate.toLocaleDateString() + ' 下午' + thedate.getHours() + '点' + thedate.getMinutes() + '分' + thedate.getSeconds() + '秒');
}
*/ </script> 
</body> 
</html>

您可能感兴趣的文章:
javascript格式化时间日期函数举例
js格式化日期时间型数据函数的实现代码
js long日期格式转为标准日期格式的代码

您可能感兴趣的文章:
js按指定格式显示日期时间的代码
php strtotime函数怎么用
javascript日期对象格式化为字符串
js如何获取指定日期前后的日期
php日期函数的简单示例代码
js获取当前时间戳与日期比较
javascript日期格式化简单例子
javascript日期计算与格式化日期
JavaScript Date(日期)对象
javascript 格式化时间日期函数代码

关键词: js日期格式化   
[关闭]