转自:http://blog.csdn.net/vbangle/article/details/5643091

 

方法一:这个很不错,好像是 csdn 的 Meizz 写的:

  1. // 对Date的扩展,将 Date 转化为指定格式的String
  2. // 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
  3. // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
  4. // 例子:
  5. // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
  6. // (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18
  7. Date.prototype.Format = function(fmt)
  8. { //author: meizz
  9. var o = {
  10. "M+" : this.getMonth()+1,                 //月份
  11. "d+" : this.getDate(),                    //日
  12. "h+" : this.getHours(),                   //小时
  13. "m+" : this.getMinutes(),                 //分
  14. "s+" : this.getSeconds(),                 //秒
  15. "q+" : Math.floor((this.getMonth()+3)/3), //季度
  16. "S"  : this.getMilliseconds()             //毫秒
  17. };
  18. if(/(y+)/.test(fmt))
  19. fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
  20. for(var k in o)
  21. if(new RegExp("("+ k +")").test(fmt))
  22. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
  23. return fmt;
  24. }

调用方法:

  1. var time1 = new Date().Format("yyyy-MM-dd HH:mm:ss");     
  2.   
  3. var time2 = new Date().Format("yyyy-MM-dd");    

方法二:

  1. <mce:script language="javascript" type="text/javascript"><!--
  2. /**
  3. * 对Date的扩展,将 Date 转化为指定格式的String
  4. * 月(M)、日(d)、12小时(h)、24小时(H)、分(m)、秒(s)、周(E)、季度(q) 可以用 1-2 个占位符
  5. * 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
  6. * eg:
  7. * (new Date()).pattern("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
  8. * (new Date()).pattern("yyyy-MM-dd E HH:mm:ss") ==> 2009-03-10 二 20:09:04
  9. * (new Date()).pattern("yyyy-MM-dd EE hh:mm:ss") ==> 2009-03-10 周二 08:09:04
  10. * (new Date()).pattern("yyyy-MM-dd EEE hh:mm:ss") ==> 2009-03-10 星期二 08:09:04
  11. * (new Date()).pattern("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
  12. */
  13. Date.prototype.pattern=function(fmt) {
  14. var o = {
  15. "M+" : this.getMonth()+1, //月份
  16. "d+" : this.getDate(), //日
  17. "h+" : this.getHours()%12 == 0 ? 12 : this.getHours()%12, //小时
  18. "H+" : this.getHours(), //小时
  19. "m+" : this.getMinutes(), //分
  20. "s+" : this.getSeconds(), //秒
  21. "q+" : Math.floor((this.getMonth()+3)/3), //季度
  22. "S" : this.getMilliseconds() //毫秒
  23. };
  24. var week = {
  25. "0" : "/u65e5",
  26. "1" : "/u4e00",
  27. "2" : "/u4e8c",
  28. "3" : "/u4e09",
  29. "4" : "/u56db",
  30. "5" : "/u4e94",
  31. "6" : "/u516d"
  32. };
  33. if(/(y+)/.test(fmt)){
  34. fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
  35. }
  36. if(/(E+)/.test(fmt)){
  37. fmt=fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "/u661f/u671f" : "/u5468") : "")+week[this.getDay()+""]);
  38. }
  39. for(var k in o){
  40. if(new RegExp("("+ k +")").test(fmt)){
  41. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
  42. }
  43. }
  44. return fmt;
  45. }
  46. var date = new Date();
  47. window.alert(date.pattern("yyyy-MM-dd hh:mm:ss"));
  48. // --></mce:script>

方法三:

  1. Date.prototype.format = function(mask) {
  2. var d = this;
  3. var zeroize = function (value, length) {
  4. if (!length) length = 2;
  5. value = String(value);
  6. for (var i = 0, zeros = ''; i < (length - value.length); i++) {
  7. zeros += '0';
  8. }
  9. return zeros + value;
  10. };
  11. return mask.replace(/"[^"]*"|'[^']*'|/b(?:d{1,4}|m{1,4}|yy(?:yy)?|([hHMstT])/1?|[lLZ])/b/g, function($0) {
  12. switch($0) {
  13. case 'd':   return d.getDate();
  14. case 'dd':  return zeroize(d.getDate());
  15. case 'ddd': return ['Sun','Mon','Tue','Wed','Thr','Fri','Sat'][d.getDay()];
  16. case 'dddd':    return ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'][d.getDay()];
  17. case 'M':   return d.getMonth() + 1;
  18. case 'MM':  return zeroize(d.getMonth() + 1);
  19. case 'MMM': return ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'][d.getMonth()];
  20. case 'MMMM':    return ['January','February','March','April','May','June','July','August','September','October','November','December'][d.getMonth()];
  21. case 'yy':  return String(d.getFullYear()).substr(2);
  22. case 'yyyy':    return d.getFullYear();
  23. case 'h':   return d.getHours() % 12 || 12;
  24. case 'hh':  return zeroize(d.getHours() % 12 || 12);
  25. case 'H':   return d.getHours();
  26. case 'HH':  return zeroize(d.getHours());
  27. case 'm':   return d.getMinutes();
  28. case 'mm':  return zeroize(d.getMinutes());
  29. case 's':   return d.getSeconds();
  30. case 'ss':  return zeroize(d.getSeconds());
  31. case 'l':   return zeroize(d.getMilliseconds(), 3);
  32. case 'L':   var m = d.getMilliseconds();
  33. if (m > 99) m = Math.round(m / 10);
  34. return zeroize(m);
  35. case 'tt':  return d.getHours() < 12 ? 'am' : 'pm';
  36. case 'TT':  return d.getHours() < 12 ? 'AM' : 'PM';
  37. case 'Z':   return d.toUTCString().match(/[A-Z]+$/);
  38. // Return quoted strings with the surrounding quotes removed
  39. default:    return $0.substr(1, $0.length - 2);
  40. }
  41. });
  42. };

(转)javascript日期格式化扩展的更多相关文章

  1. JavaScript 日期格式化 简单有用

    JavaScript 日期格式化 简单有用 代码例如以下,引入jquery后直接后增加下面代码刷新可測试 Date.prototype.Format = function (fmt) { //auth ...

  2. Javascript 日期格式化

    Javascript 日期格式化 需求: 给出:日期 .格式,根据日期格式进行输出. Date.prototype.Format = function (fmt) { //author: meizz ...

  3. javascript日期格式化方法汇总

    本文给大家汇总介绍了javascript格式化日期时间的几种常用方法,个人对最后一种个性化输出时间比较有兴趣,基本上只要项目中能用到都是使用这种,推荐给小伙伴们. 方法一: ? 1 2 3 4 5 6 ...

  4. js日期格式化 扩展Date()

    javascript Date format(js日期格式化) 方法一: // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(H/h).分(m).秒(s ...

  5. 一个JavaScript日期格式化扩展函数

    我们都知道在Java和PHP语言中,有专门用于格式化日期对象的类和函数,例如Java中的DateFormat等等,通过这些类和函数,我们可以方便的将一个日期对象按照格式的要求输出为字符串,例如对于同一 ...

  6. Javascript 日期格式化 相关操作

    1.相关扩展函数 //--------------------------------------------------- // 判断闰年 //--------------------------- ...

  7. Javascript日期格式化指定格式的字符串实现

    代码部分 TypeScript /** * format a Date object * 将 Date 转化为指定格式的String * @param {Date} date 源日期对象 * @par ...

  8. JS日期格式化扩展

    1.扩展 //扩展日期 Date.prototype.Format = function (fmt) { //author: meizz var o = { , //月份 "d+" ...

  9. 轻松搞定javascript日期格式化问题

    Date.prototype.format = function(f){ var d = this f = f || "yyyy-MM-dd hh:mm:ss" return f. ...

随机推荐

  1. python 学习定时任务apscheduler模块

    最近在解决定时任务问题找到了apscheduler模块,贴一段代码 from apscheduler.schedulers.blocking import BlockingSchedulerimpor ...

  2. 《Linux内核设计与实现》笔记-1-linux内核简单介绍

    一.Linux内核相对于传统的UNIX内核的比較: (1):Linux支持动态内核模块. 虽然Linux内核也是总体式结构,但是同意在须要的时候动态哦卸除(rmmod xxx)和载入内核模块(insm ...

  3. mysql 严格模式取消 group by 和 date zore

    取消单个库的时间严格模式 set global sql_mode=(select replace(@@sql_mode,'NO_ZERO_IN_DATE,NO_ZERO_DATE',''));

  4. Hibernate 主配置文件详解

    摘要: 版权声明:本文为博主原创文章,如需转载请标注转载地址. 博客地址:http://www.cnblogs.com/caoyc/p/5595870.html 一.主配置文件命名规则 1.默认名称: ...

  5. hibernate 多对多双向关联

    package com.bjsxt.hibernate; import java.util.HashSet; import java.util.Set; import javax.persistenc ...

  6. SuperMap iClient如何使用WMS地图服务

    什么是WMS服务 WMS(Web Map Service,Web 地图服务)服务,该服务符合 OGC(Open Geospatial Consortium,开放地理信息联盟)制定的 WMS 实现规范. ...

  7. Linux-使用 screen 管理你的远程会话

    转自:http://www.ibm.com/developerworks/cn/linux/l-cn-screen/ 你是不是经常需要 SSH 或者 telent 远程登录到 Linux 服务器?你是 ...

  8. 【Shiro】Apache Shiro架构之权限认证(Authorization)

    Shiro系列文章: [Shiro]Apache Shiro架构之身份认证(Authentication) [Shiro]Apache Shiro架构之集成web [Shiro]Apache Shir ...

  9. android的五大布局(layout)

    Android的界面是有布局和组件协同完成的,布局好比是建筑里的框架,而组件则相当于建 筑里的砖瓦.组件按照布局的要求依次排列,就组成了用户所看见的界面.Android的五大布局分别是LinearLa ...

  10. java通过CLASSPATH读取包内文件

    读取包内文件,使用的路径一定是相对的classpath路径,比如a,位于包内,此时可以创建读取a的字节流:InputStream in = ReadFile.class.getResourceAsSt ...