javascript Date format(js日期格式化)

方法一:

 // 对Date的扩展,将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(H/h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
// 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// 例子:
// getNewDate("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// getNewDate("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
// getNewDate("yy-M-d h:m:s.S") ==> 06-7-2 8:9:4.18 function getNewDate(Format)
{
var myDate = new Date();
var objFormat={
"M+":myDate.getMonth()+1,
"d+":myDate.getDate(),
"H+":myDate.getHours(),
"h+":myDate.getHours(),
"m+":myDate.getMinutes(),
"s+":myDate.getSeconds(),
"q+":Math.floor((myDate.getMonth()+3)/3),
"S+":myDate.getMilliseconds()
};
if(/(y+)/.test(Format))
{
Format=Format.replace(RegExp.$1,(myDate.getFullYear()+'').substr(4-RegExp.$1.length));
for(var format in objFormat)
{
if(new RegExp("("+format+")").test(Format))
{
Format=Format.replace(RegExp.$1,(RegExp.$1.length==1)?objFormat[format]:('00'+objFormat[format]).substr((''+objFormat[format]).length));
}
}
}
return Format;
} <!--调用实例:-->
<!DOCTYPE html>
<html>
<head>
<title>js_test_016</title>
<script src="js_import_program/test_import_001.js"></script>
</head>
<body>
<h1>测试new Date()</h1>
<p><b>输入的日期格式:</b></p><input type="text" id="dateFormat" style="width:180px"/></br>
<p><b>返回的日期格式:</b></p><input type="text" id="dateResult" style="width:180px"/></br>
<button type="button" onclick="getDate()">显示日期</button>
<script>
function getDate()
{
var dateformat=document.getElementById('dateFormat').value;
if(dateformat!=null&&dateformat!='')
{
var resultDate=getNewDate(dateformat);
if(resultDate!=null&&resultDate!='')
{
document.getElementById('dateResult').value=resultDate;
}
else
{
alert('返回日期为空!');
}
}
else
{
alert('日期格式不能为空');
}
}
</script>
</body>
</html>

代码N01

方法二:

 <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>

代码N02

方法三:

 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); } }); };

代码N03

原文地址:http://www.cnblogs.com/zhangpengshou/archive/2012/07/19/2599053.html

最新文章

  1. Codeforces Round #133 (Div. 2)
  2. Dictionary 的使用
  3. 如何为 Drupal 7 网站添加悬浮的反馈按钮?
  4. 在centOS中加入本地ISO yum源
  5. UVa 10870 (矩阵快速幂) Recurrences
  6. iOS app应用界面加载卡顿的问题
  7. 网络爬虫-url索引
  8. (原创)openvswitch实验连载2-cisco模拟器IOU-Web安装及网络环境配置
  9. 控制结构(8) 线性化(linearization)
  10. 【集训队作业2018】取名字太难了 任意模数FFT
  11. CodeForces5E 环转链,dp思想
  12. effective java——31用实例域代替序数
  13. 2019-04-11-day030-网络编程并发
  14. OpenStack构架知识梳理
  15. monobehaviour生命周期完整版
  16. JVM总结(一):概述--JVM对象探秘
  17. tomcat manager
  18. UEdit百度富文本编辑器
  19. 51nod 1443 路径和树(最短路)
  20. 最新Flume1.7 自定义 MongodbSink 结合TAILDIR Sources的使用

热门文章

  1. np.ones(N)/N的作用
  2. Java---SSH(MVC)面试
  3. LG3389 【模板】高斯消元法
  4. ①Jenkins集成—入门安装使用
  5. LINQ to SQL 系列 如何使用LINQ to SQL插入、修改、删除数据 (转)
  6. equals方法和==的区别
  7. 使用C++生成1-33中的6个随机数,无重复
  8. java 线程池 ExeutorService
  9. CentOS6.5 安装codeblocks-13.12
  10. solr之高级查询--联表 join查询