js 格式化 时间插件
2024-08-27 21:02:22
// 对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 time1 = new Date().Format("yyyy-MM-dd");var time2 = new Date().Format("yyyy-MM-dd HH:mm:ss");
最新文章
- [Sass]混合宏
- Maven详解之聚合与继承
- struts2 拦截器 interceptor
- JAVA 继承 extends
- 屏幕分辨率与FPS
- 函数mem_area_alloc
- 深入研究Block用weakSelf、strongSelf、@weakify、@strongify解决循环引用(上)
- IOPS=(Queue Depth)/(IO latency)
- poj 3262 Protecting the Flowers 贪心
- java中a++与++a区别
- Chapter 2 Open Book——17
- TForm类
- windows 系统验证是否为正版
- shiro的rememberMe不生效
- 从零开始学 Web 之 JS 高级(三)apply与call,bind,闭包和沙箱
- mysql复习之一
- SqlServer执行大的数据库脚本出错解决方法
- MonoBehaviour类Invoke, Coroutine
- c++日志输出库 spdlog 简介(3)多线程控制台输出日志
- Android之开源项目汇总