// 对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) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
return fmt;
}
 //使用方法
var now = new Date();
var nowStr = now.format("yyyy-MM-dd hh:mm:ss");
//使用方法2:
var testDate = new Date();
var testStr = testDate.format("YYYY年MM月dd日hh小时mm分ss秒");
alert(testStr);
//示例:
alert(new Date().format("yyyy年MM月dd日"));
alert(new Date().format("MM/dd/yyyy"));
alert(new Date().format("yyyyMMdd"));
alert(new Date().format("yyyy-MM-dd hh:mm:ss"));
 

最新文章

  1. 闲话Promise机制
  2. ios9-通讯录
  3. cocoapods的时候出现的问题 _OBJC_CLASS_$_XXX
  4. Entity Framework数据库初始化四种策略
  5. TStringList 常用操作
  6. linux入命令基础
  7. BZOJ4012 [HNOI2015]开店
  8. KAFKA分布式消息系统
  9. Javascript 解构的用处
  10. Uva 10935 Throwing cards away I
  11. 【.NET】上传文件,生成缩略图
  12. Java Web入门学习(四)Eclipse与Maven、Tomcat整合配置
  13. ffmpeg.exe下载
  14. Learn jQuery in y seconds
  15. L - The Shortest Path Gym - 101498L (dfs式spfa判断负环)
  16. ADO.NET系列之事务和调用存储过程
  17. android开发(44) 使用了 SoundPool 播放提示音
  18. rman 中遇到 ORA-01861
  19. BZOJ3940:[USACO]Censoring(AC自动机,栈)
  20. 新人成长之入门Vue.js常用指令介绍(一)

热门文章

  1. 【ACM】拦截导弹 - 0-1背包问题
  2. LL_01
  3. Gym 101047M Removing coins in Kem Kadrãn
  4. Unity Time.timeScale
  5. (转)linux route命令详解
  6. 在loader程序中涉及到的CPU模式切换
  7. JDK 7 API 下载
  8. SpringBoot | 第五章:多环境配置
  9. WCF 内置绑定在不同的传输安全模式下的信道层
  10. 【踩坑】vue 无法让后台保存 session