方法一:这个很不错,好像是 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. Uva 11395 Sigma Function (因子和)

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=109329#problem/C   题目在文末 题意:1~n (n:1~1012)中,因子 ...

  2. leetcode-Excel Sheet Column Title

    题目: 把数字转化为excel形式的字符表示.示例:1->A 2->B 3->C ... 26->Z 27->AA... 解题思路: 乍一看有点像进制转换题目,不过细想想 ...

  3. 第66课 C++中的类型识别

    1. 类型识别 (1)在面向对象中可能出现下面的情况 ①基类指针指向子类对象 ②基类引用成为子类对象的别名 ▲静态类型——变量(对象)自身的类型(定义变量类型时类型或参数类型) ▲动态类型——指针(引 ...

  4. u3d_Shader_effects笔记4 BRDF

    1.英文意思 bidirectional reflectance distribution function bidirectional :双向的 reflectance :反射 distributi ...

  5. HTML 学习笔记 CSS3(Animation)

    CSS3动画: 通过CSS3 我们能够创建动画 这可以在许多网页中取代动画图片 Flash动画 以及JavaScript. CSS3 @keyframes 规则如需在 CSS3 中创建动画,您需要学习 ...

  6. APIO2015泛做

    可以在UOJ上提交也可以在bzoj上提交(权限) A. Bali Sculptures 对于前72%的数据,按位考虑,然后跑一点沙茶dp就行了. dp:用f[x][y]表示前x位分为y段是否满足条件. ...

  7. django自带wsgi server vs 部署uwsgi+nginx后的性能对比

    一.下面先交代一下测试云主机 cpu: root@alexknight:/tmp/webbench-1.5# cat /proc/cpuinfo |grep model model : model n ...

  8. Java的super调用案例: super.getClass()返回的是子类自己

    If you override a method from your superclass (or your superclass's superclass etc.), super.theMetho ...

  9. mvc5 Html.EditorFor html属性有了新变化,和以前的不同了

    @Html.EditorFor(model => model.MaxNumber, new { htmlAttributes = new { @min = "1" } })

  10. Zepto的天坑汇总

    前言 最近在做移动端开发,用的是zepto,发现他跟jquery比起来称之为天坑不足为过,但是由于项目本身原因,以及移动端速度要求的情况下,也只能继续用下去. 所以在这里做一下汇总 对img标签空sr ...