• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
公子缘
博客园    首页    新随笔    联系   管理    订阅  订阅

Javascript 日期格式化

Javascript 日期格式化

 

需求:

  给出:日期 、格式,根据日期格式进行输出。

 1 Date.prototype.Format = function (fmt) { //author: meizz 
 2                         var o = {
 3                             "M+": this.getMonth() + 1, //月份
 4                             "d+": this.getDate(), //日
 5                             "h+": this.getHours(), //小时
 6                             "m+": this.getMinutes(), //分
 7                             "s+": this.getSeconds(), //秒
 8                             "q+": Math.floor((this.getMonth() + 3) / 3), //季度
 9                             "S": this.getMilliseconds() //毫秒
10                         };
11                         if (/(y+)/.test(fmt)) {
12                             fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
13                         }
14                         for (var k in o) {
15                             if (new RegExp("(" + k + ")").test(fmt)) {
16                                 fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
17                             }
18                         }
19                         return fmt;
20                     }

 

使用代码:

1 new Date(new Date(), 'yyyy-MM-dd hh:mm:ss');

 

若现在就觉得失望无力,未来那么远你该怎么扛 —— 致自己
posted @ 2019-06-25 16:54  公子缘  阅读(333)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3