# 格式化日期和时间

扩展 Date:

Date.prototype.format = function(format){
var o = {
"M+" : this.getMonth()+1, //month
"d+" : this.getDate(), //day
"h+" : this.getHours(), //hour
"m+" : this.getMinutes(), //minute
"s+" : this.getSeconds(), //second
"q+" : Math.floor((this.getMonth()+3)/3), //quarter
"S" : this.getMilliseconds() //millisecond
}

if(/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
}

for(var k in o) {
if(new RegExp("("+ k +")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
}
}
return format;
}

测试:

var testDate = new Date();

//yyyy-MM-dd hh:mm:ss
var testDateStr = now.format("yyyy-MM-dd hh:mm:ss");
console.log(testDateStr);

//YYYY年MM月dd日hh小时mm分ss秒
var testDateStr = testDate.format("YYYY年MM月dd日hh小时mm分ss秒");
console.log(testDateStr);

console.log(new Date().Format("yyyy年MM月dd日"));
console.log(new Date().Format("MM/dd/yyyy"));
console.log(new Date().Format("yyyyMMdd"));
console.log(new Date().Format("yyyy-MM-dd hh:mm:ss"));

最新文章

  1. 【知识必备】ezSQL,最好用的数据库操作类,让php操作sql更简单~
  2. GetWord 3.3 屏幕取词
  3. .net配置文件读取
  4. js 导出Excel
  5. reinstall_xbt: Linux中如何查看文件的最初创建时间
  6. [实变函数]5.3 非负可测函数的 Lebesgue 积分
  7. BCTF warmup 50
  8. jvm(13)-线程安全与锁优化(转)
  9. jquery让页面滚动条top,滚动条 顶部
  10. AugularJS1.X不升级到2.X
  11. ASP.NET Core中使用自定义MVC过滤器属性的依赖注入
  12. Swagger Edit 安装和使用教程
  13. JAVA时间工具类,在维护的项目里的
  14. 一些Java基础方面问题的总结
  15. redis安全问题【原】
  16. 论文笔记:Rich feature hierarchies for accurate object detection and semantic segmentation
  17. Pick-up sticks
  18. 著名软件工程师与作家、极限编程的创始者、JUnit作者之Kent Beck
  19. [ios]关于ios开发图片尺寸的建议
  20. wamp安装和基础配置

热门文章

  1. 用 Flask 来写个轻博客 (33) — 使用 Flask-RESTful 来构建 RESTful API 之二
  2. 第一步:卸载手机上的“WIFI万能钥匙”! 会分享自己家的wifi
  3. 进程管理工具-Supervisord 使用
  4. CentOS 7下升级python版本到3.X
  5. 面试题22:链表中倒数第k个节点
  6. 转 zookeeper,dubbo和Nginx的区别
  7. Python面试题之如何用Python来发送邮件?
  8. 53.Coin Change(找硬币)
  9. 2019牛客暑期多校训练营(第五场) maximum clique 1
  10. php 字符转成数字