zoukankan      html  css  js  c++  java
  • JS日期格式化

    JS日期格式化

    在JS中添加自定义format函数(编辑器改成了Markdown突然不习惯了)

        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() + "").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;
    }
    

    调用:

    new Date().format("yyyy-MM-dd");
    new Date().format("yyyy-MM-dd hh:mm:ss");
    如果对你有帮助,点个"推荐"呗~
    如果发现不当之处,欢迎不吝赐教~
    转载请注明出处哦~
  • 相关阅读:
    test
    c# cook book -Linq 关于Object的比较
    关于UnitOfWork
    autofac学习
    webapi 开启跨域支持
    httpclient通过post提交到webapi
    jQuery之元素查找
    jQuery之过滤元素
    jQuery之回到顶部
    jQuery之_元素滚动
  • 原文地址:https://www.cnblogs.com/imone/p/9154801.html
Copyright © 2011-2022 走看看