JS-日期和时间
# 格式化日期和时间
扩展 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"));
最新文章
- 【知识必备】ezSQL,最好用的数据库操作类,让php操作sql更简单~
- GetWord 3.3 屏幕取词
- .net配置文件读取
- js 导出Excel
- reinstall_xbt: Linux中如何查看文件的最初创建时间
- [实变函数]5.3 非负可测函数的 Lebesgue 积分
- BCTF warmup 50
- jvm(13)-线程安全与锁优化(转)
- jquery让页面滚动条top,滚动条 顶部
- AugularJS1.X不升级到2.X
- ASP.NET Core中使用自定义MVC过滤器属性的依赖注入
- Swagger Edit 安装和使用教程
- JAVA时间工具类,在维护的项目里的
- 一些Java基础方面问题的总结
- redis安全问题【原】
- 论文笔记:Rich feature hierarchies for accurate object detection and semantic segmentation
- Pick-up sticks
- 著名软件工程师与作家、极限编程的创始者、JUnit作者之Kent Beck
- [ios]关于ios开发图片尺寸的建议
- wamp安装和基础配置
热门文章
- 用 Flask 来写个轻博客 (33) — 使用 Flask-RESTful 来构建 RESTful API 之二
- 第一步:卸载手机上的“WIFI万能钥匙”! 会分享自己家的wifi
- 进程管理工具-Supervisord 使用
- CentOS 7下升级python版本到3.X
- 面试题22:链表中倒数第k个节点
- 转 zookeeper,dubbo和Nginx的区别
- Python面试题之如何用Python来发送邮件?
- 53.Coin Change(找硬币)
- 2019牛客暑期多校训练营(第五场) maximum clique 1
- php 字符转成数字