在上篇文章给大家介绍了js对Date对象的操作的问题(生成一个倒数7天的数组),本篇介绍有关js日期格式化之javascript Date format,本文通过三种方法给大家讲解,具体内容请看下文。

方法一:

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

调用方式:

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

方法二:

<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 "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) "/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) "00"+ o[k]).substr((""+ o[k]).length)));   
  }   
 }   
 return fmt;   
}   
var date = new Date();  
window.alert(date.pattern("yyyy-MM-dd hh:mm:ss"));
// -->
</script>

方法三:

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

以上内容是本文给大家介绍的JS日期格式化之javascript Date format,希望大家喜欢。

标签:
js_date格式化,js_date_format

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
评论“JS日期格式化之javascript Date format”
暂无“JS日期格式化之javascript Date format”评论...

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。