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");
    如果对你有帮助,点个"推荐"呗~
    如果发现不当之处,欢迎不吝赐教~
    转载请注明出处哦~
  • 相关阅读:
    使用DataReader
    使用Dataset
    Command
    Ado.net对象
    集合数据源
    Label 表达式绑定
    输出二进制图像
    Application 可以存储全局变量
    字符串处理
    ?:叫条件运算符
  • 原文地址:https://www.cnblogs.com/imone/p/9154801.html
Copyright © 2011-2022 走看看