DateUtil时间格式化工具类

news/2024/7/19 15:14:58 标签: js
    var DateUtil={
        /**
         * 当前时间,格式 yyyy-MM-dd HH:mm:ss
         * @return 当前时间的标准形式字符串
         */
        now: function() {
            return new Date().format("yyyy-MM-dd HH:mm:ss");
        },
        /**
         * 格式化日期时间
         * 格式 yyyy-MM-dd HH:mm:ss
         * @param date 被格式化的日期
         * @param format 格式化 参考 {@link date_formate}
         * @return 格式化后的日期
         */
        formatDateTime: function(date,format) {
            if(format == undefined || format ==null ) {
                format = "yyyy-MM-dd HH:mm:ss";
            }
            return date.format(format);
        },
        /**
         * 只支持毫秒级别时间戳,如果需要秒级别时间戳,请自行×1000
         * @param timestamp 时间戳
         * @return 时间对象
         */
        date:function (timestamp) {
            return new Date(timestamp);
        }
    };

    /** 日期格式 命名参考hutool工具类 */
    date_formate = {
        /** 标准日期格式:yyyy-MM-dd */
        normDatePattern: "yyyy-MM-dd",
        /** 标准时间格式:hh:mm:ss */
        normTimePattern : "HH:mm:ss",
        /** 标准日期时间格式,精确到分:yyyy-MM-dd HH:mm */
        normDatetimeMinutePattern:"yyyy-MM-dd HH:mm",
        /** 标准日期时间格式,精确到秒:yyyy-MM-dd HH:mm:ss */
        normDatetimePattern:"yyyy-MM-dd HH:mm:ss",
        /** 标准日期时间格式,精确到毫秒:yyyy-MM-dd HH:mm:ss.SSS */
        normDatetimeMsPattern:"yyyy-MM-dd HH:mm:ss.SSS",
        /** 标准日期格式:yyyy年MM月dd日 */
        chineseDatePattern : "yyyy年MM月dd日",
        /** 标准日期格式:yyyyMMdd */
        pureDatePattern : "yyyyMMdd",
        /** 标准日期格式:HHmmss */
        pureTimePattern : "HHmmss",
        /** 标准日期格式:yyyyMMddHHmmss */
        pureDatetimePattern : "yyyyMMddHHmmss",
        /** 标准日期格式:yyyyMMddHHmmssSSS */
        pureDatetimeMsPattern : "yyyyMMddHHmmssSSS",
    };

    Date.prototype.format = function(fmt) {
        var o = {
            "M+" : this.getMonth()+1,                 //月份
            "d+" : this.getDate(),                    //日
            "H+" : this.getHours(),                   //小时
            "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;
    };

http://www.niftyadmin.cn/n/1867084.html

相关文章

171113 Learning Python Chapter 27 Learning Python A More Realistic Example

Chapter 25 → 类的蓝图 ↓Chapter 26 → 类的基础 ↓Chapter 27 → 类的实例 ↓Chapter 28 → 类的细节 本章中,我们编写两个类: - Person 创建并处理关于人员信息的一个类 - 定制一个Person,修改继承的行为 步骤1:创建实…

MySQL(InnoDB)高级篇(持续更新)

文章目录MySQL高级篇(InnoDB)1.索引1.InnoDB存储引擎----B Tree索引2.索引分类3.聚簇索引、非聚簇索引4. InnoDB存储引擎---hash索引5. 使用InnoDB,为什么推荐使用自增的整数类型作为主键?6.复合索引最左前缀原则最左前缀&#xf…

layui中table表格内容过多自动换行

表格超出内容没有自动换行,超出部分需要自动换行。 允许长单词换行到下一行:p.test {word-wrap:break-word;}

171121 Differences between the L1-norm and the L2-norm

Differences between the L1-norm and the L2-norm As an error function train for parameters L1−norm:S∑i1n|yi−f(xi)|L2−norm:S∑i1n(yi−f(xi))2As an Regularization prevent overfitting L1−regularization:w∗argminw∑j(t(xj)−∑iwihi(xj))2λ∑i1k|wi|L2…

layui单/复选框设置值和获取值

//layui单/复选框设置值和获取值layui.use(form,function(){var sex男;var formlayui.form;from.on(submit(submit),function(data){//设置值//radio$([namecheckedbox]).each(function (i,item) {if($(item).val()sex){$(item).prop(ehecked,true/false);}})//checkbox$("…

MySQL基础备用篇

MySQL 1.常用SQL语法 1.加号 在mysql中 只能是运算符 如果将两个字段用 相连,并且两个字段都不是数值类型(会尝试将字符类型转为数值,"123"这种纯数字的字符串就能被转为123,而不全是数值的字符串"123aaa"被…

java中计算两个日期之间相差多少天

java中计算两个日期之间相差多少天 先将Date格式化为只有年、月、日的形式,返回String。然后将此String再转化为Date(此时时、分、秒就成为了00:00:00)调用getTime()方法返回Date的1970的毫秒表示。将两个日期的毫秒相减,然后除以一天有&…

阿里oss上传的txt文件浏览器直接打开会乱码

阿里oss上传的txt文件浏览器直接打开会乱码 原因: txt文件在windows中默认采用ANSI编码方式。我们是采用阿里云oss方式存储文件的,需要下载时,在前端自定义a标签进行下载。浏览器默认直接打开txt文件,但浏览器的编码是utf-8,这样…