zoukankan      html  css  js  c++  java
  • JS时间格式化


    接受两个参数: (_date,_format)

    • _date:

      日期对象或日期格式字符串

    • _format:

      返回的格式:

      • y:年

      • M:月

      • d:日

      • h:时

      • m:分

      • s:秒

      • S:季度

    • 示例:dateFormat(new Date(),"yyyy-MM-dd")


    function dateFormat(_data, _format) {
        if (typeof _data == "string") {
            let _m = _data.match(/(/Date((d+))/)/);
            if (_m && _m.length >= 3) {
                _data = parseInt(_m[2]);
            }
        }
        let data = new Date(_data);
        if (!data || data.toUTCString() == "Invalid Date") return "";
        let map = {
            "M": data.getMonth() + 1, // 月份
            "d": data.getDate(), // 日
            "h": data.getHours(), // 小时
            "m": data.getMinutes(), // 分
            "s": data.getSeconds(), // 秒
            "q": Math.floor((data.getMonth() + 3) / 3), // 季度
            "S": data.getMilliseconds() // 毫秒
        };
        let format = _format.replace(/([yMdhmsqS])+/g, function (all, t) {
            let _v = map[t];
            if (_v !== undefined) {
                if (all.length > 1) {
                    _v = "0" + _v;
                    _v = _v.substr(_v.length - 2);
                }
                return _v;
            } else if (t === "y") {
                return (data.getFullYear() + "").substr(4 - all.length);
            }
            return all;
        })
        return format;
    }
    
  • 相关阅读:
    题目3:爬楼梯
    题目1:删除排序数组中的重复数字
    最近目标
    软件工程----个人总结
    软件工程第二次作业——结对编程
    软件工程第一次作业补充
    爬楼梯
    买卖股票的最佳时机
    删除排序数组中的重复数字
    思考题
  • 原文地址:https://www.cnblogs.com/langkyeSir/p/13258624.html
Copyright © 2011-2022 走看看