zoukankan      html  css  js  c++  java
  • 时间格式化 Date-formatDate

    //日期格式化
    
    export function formatDate(date,fmt){
        var o = {
            "M+":date.getMonth() + 1,//月份
            "D+":date.getDay(),//
            "h+":date.getHours(),//hours
            "m+":date.getMinutes(),//分钟
            's+':date.getSeconds(),//秒,
        }
    
        if(/(y+)/.test(fmt)){
            //RegExp.$1 是RegExp的一个属性,指的是与正则表达式匹配的第一个 子匹配(以括号为标志)字符串,以此类推,RegExp.$2,RegExp.$3,..RegExp.$99总共可以有99个匹配
            fmt = fmt.replace(RegExp.$1,(date.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;
    }

    调用:
    项目中:import {formatDate} from "./formatDate.js"
    js中:formate(new Date(),'yyyy-MM-DD hh:mm:ss')
  • 相关阅读:
    ThreadLocal总结
    zookeeper学习笔记
    安装solr
    VirtualBox安装Centos7
    Solr学习
    Redis缓存会出现的问题?
    面试题目总结
    mysql分库分表
    Java内存模型
    HashMap在多线程中循环链表问题(jdk1.7)
  • 原文地址:https://www.cnblogs.com/huangmin1992/p/8066785.html
Copyright © 2011-2022 走看看