首页 > 其他分享 >js封装时间格式化

js封装时间格式化

时间:2022-10-15 14:33:06浏览次数:44  
标签:封装 format fmt js yyyy new Date RegExp 格式化

 

/**
  * 方法
  * @description 对Date的扩展,将 Date 转换为指定格式的String
  *  月(M)、日(d)、小时(H)、分(m)、秒(s)、季度(q) 可以用 1~2 个占位符,
  *  年(y)可以用 1~4 个占位符,毫秒(S)只能用 1 个占位符(是 1~3 位的数字)
  * @param fmt
  * @example    * (new Date()).format("yyyy-MM-dd HH:mm:ss") 
  * (new Date()).format("yyyy-M-d H:m")  
  * @returns {*}
  */
Date.prototype.format = function (fmt) {
    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() + "").substring(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]:o[k]+''.padStart(0,2))
        }
    }
    return fmt;
};

var d = new Date();
console.log(d.format('yyyy-MM-dd HH:mm:ss.S')); // 2022-10-15 14:26:39.499
console.log(d.format('yyyy-M-d H:m')); // 2022-10-15 14:26

 

标签:封装,format,fmt,js,yyyy,new,Date,RegExp,格式化
From: https://www.cnblogs.com/ddqyc/p/16794172.html

相关文章

  • [Java] jackson 和 fastjson 处理 JSON对比
    JSON字符串->对象处理原型数值实体类@Setter@Getter@ToStringpublicclassInfo{privateintage;}测试代码@TestpublicvoidtestString()throwsJso......
  • VScode格式化C语言程序时,让左大括号不换行的解决方案
    前言继上次用VScode写C语言之后,感觉舒服了不少,可是还是有一点让我觉得美中不足的地方……那就是!每次格式化C语言程序的时候,都会把我故意不换行的左大括号给换行了!后来找......
  • [Unit testing RxJS] Test complex logic with time progression syntax
    Forexamplewehaveasearchinput:constinput$=fromEvent(document.getElementById("#input"),"input");input$.pipe(debounceTime(200),pluck("ta......
  • [Unit testing RxJS] Overview: Testing Observables with Marble Testing
    const{TestScheduler}=require("rxjs/testing");const{map,take,delay,mapTo,catchError}=require("rxjs/operators");const{concat,from,of,interva......
  • VScode保存自动格式化
    一、配置1.点击左下角齿轮,打开设置2.在文本编辑器里下滑找到Code Actions On Save下面的在setting.json中编辑3.复制代码全部替换{ "eggHelper.serverPort":62......
  • js-dom
    DOM的概述DOM(documentobjectmodel)文档对象模型,顾名思义他就是用于操作对应的文档的,也就是操作你写的html文档。DOM是一个遵从文档流的语句。所以他是同步机制。DOM的分......
  • RxJS 系列 – 实战练习
    前言这篇主要是给一些简单例子,从中体会RxJS在管理上的思路.  SlideDownEffectwithDynamicContent我在这篇 CSS&JSEffect–FAQAccordion&SlideDow......
  • [Unit testing RxJS] Test error handling with marbles
    const{TestScheduler}=require("rxjs/testing");const{map,take,delay,mapTo,catchError}=require("rxjs/operators");const{concat,from,of}=requ......
  • JS打包简史
    目录暗黑阶段黎明阶段时间线BrowserifyWebpackRollupParcelSwcEsbuildWhyisesbuildfast?Esbuild-loaderSnowpackVite引用链接原文地址了解打包的历史可以更好的知道......
  • js querySelector选#ID报错
    code:document.querySelector("#02b51fd4-c350-65d3-c59a-cfdc5a538066")err:UncaughtDOMException:Document.querySelector:'#02b51fd4-c350-65d3-c59a-cfdc5a5380......