方法一:这个很不错,好像是 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 Date format(js日期格式化)的更多相关文章

  1. [荐]javascript Date format(js日期格式化)

    cnblog:http://www.cnblogs.com/zhangpengshou/archive/2012/07/19/2599053.html 方法一: // 对Date的扩展,将 Date  ...

  2. javascript Date format(js日期格式化) 转载

    本文转载地址http://www.cnblogs.com/zhangpengshou/archive/2012/07/19/2599053.html // 对Date的扩展,将 Date 转化为指定格 ...

  3. coding++ :javascript Date format (js日期格式化)

    方式一: // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1 ...

  4. javascript Date format(js日期格式化) (转)

    方法一:这个很不错,好像是 csdn 的 Meizz 写的: // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) ...

  5. javascript Date format(js日期格式化)

    这个用这比较爽,记录一下// 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年( ...

  6. 【JavaScript】 knockout.js 日期格式化借助【momentjs】

    源:Knockout.js 日期格式化 源:momentjs

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

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

  8. 161226、js日期格式化

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

  9. JS获取当前日期时间及JS日期格式化

    Js获取当前日期时间: var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份( ...

随机推荐

  1. STL vector

    STL vector vector是线性容器,它的元素严格的按照线性序列排序,和动态数组很相似,和数组一样,它的元素存储在一块连续的存储空间中,这也意味着我们不仅可以使用迭代器(iterator)访问 ...

  2. Windows 10 UWP开发:如何不让界面卡死

    http://edi.wang/post/2016/2/18/windows-10-uwp-async-await-ui-thread 关于UI线程 这里我们需要一点关于 UI 线程模型的概念,简单的 ...

  3. 转: Eclipse 分屏显示同一个文件

    Eclipse 分屏显示同一个文件   场景 : 某个类很大,可能有数千行.当你想要将类开头部分与中间或者靠后的部分进行对比时,请follow如下步骤: Window -> Editor -&g ...

  4. cookie操作(jquery的cookie插件源码)

    cookie : function (key, value, options) { var days, time, result, decode; // A key and value were gi ...

  5. BZOJ 1304: [CQOI2009]叶子的染色

    1304: [CQOI2009]叶子的染色 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 566  Solved: 358[Submit][Statu ...

  6. PHP提升echo, printf, print, file_put_contents等输出方法的效率

    让ECHO变快, 让PHP的请求处理过程, 尽快结束, 之所以ECHO慢, 是在等待”写数据”成功返回, 那么一个比较简单的办法, 就是打开输出缓存, 编辑php.ini output_bufferi ...

  7. SQL server 专业词汇

    sql组成:DDL:数据库模式定义语言,关键字:createDML:数据操纵语言,关键字:Insert.delete.updateDCL:数据库控制语言 ,关键字:grant.removeDQL:数据 ...

  8. MYSQL临时表创建索引

    DROP TEMPORARY TABLE IF EXISTS tmp_record_t2;CREATE TEMPORARY TABLE tmp_record_t2(consumption_id INT ...

  9. mac 10.9.4下配置apache

    mac 10.9.x已经自带了apache,可按如下步骤开启: 1.启动 sudo apachectl start 启动后,访问 http://localhost/ 应该能看到"It wor ...

  10. WPF:自动执行"机器人"程序若干注意事项

    企业应用中,经常会遇到一些需要定时自动执行的程序来完成某些功能,比如:自动定时从第三方web service取回数据.定时对历史数据进行清理.定时向ftp上传业务数据... 这类程序,我习惯称为“机器 ...