javascript Date format,js日期格式化

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

[javascript]view plaincopy

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

调用方法:

[javascript]view plaincopy

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

方法二:

[javascript]view plaincopy

  1. <mce:script language="<a href="http://lib.csdn.net/base/javascript" class='replace_word' title="JavaScript知识库" target='_blank' >JavaScript</a>" type="text/<a href="http://lib.csdn.net/base/javascript" class='replace_word' title="JavaScript知识库" target='_blank' >javascript</a>"><!--
  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>

方法三:

[javascript]view plaincopy

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