zoukankan      html  css  js  c++  java
  • js时间转换事件

    /*************************************** 数据类型转换相关 ***************************************/
        Convert: {
            dateToString: function (date) {
                if (date == null || date == undefined || date == "") {
                    return "";
                }
                /// <summary>将日期转换成 yyyy-MM-dd 格式的字符串</summary>
                /// <param name="date" type="Date">日期对象</param>
                /// <returns type="String">转换后的字符串</returns>
    
                return date.getFullYear() + "-" + (date.getMonth() + 1).toString().fill(2, "0") + "-" + date.getDate().toString().fill(2, "0");
            },
            dateTimeToString: function (date) {
                if (date == null || date == undefined || date == "") {
                    return "";
                }
                /// <summary>将日期转换成 yyyy-MM-dd 格式的字符串</summary>
                /// <param name="date" type="Date">日期对象</param>
                /// <returns type="String">转换后的字符串</returns>
    
                return date.getFullYear() + "-" + (date.getMonth() + 1).toString().fill(2, "0") + "-" + date.getDate().toString().fill(2, "0") + " " + date.getHours().toString().fill(2, "0") + ":" + date.getMinutes().toString().fill(2, "0") + ":" + date.getSeconds().toString().fill(2, "0");
            },
            stringToDate: function (str) {
                if (str == null || str == undefined || str == "") {
                    return "";
                }
                /// <summary>将 yyyy-MM-dd 格式的日期字符串转换成Date对象</summary>
                /// <param name="str" type="String">要转换的字符串</param>
                /// <returns type="Date">日期对象</returns>
    
                var str = str.replace(/-/g, "/");
                return new Date(str);
            },
            jsonStrToDate: function (str) {
                if (str == null || str == undefined || str == "") {
                    return "";
                }
                /// <summary>将 /Date(1334766600000)/ 格式的日期字符串转换成Date对象</summary>
                /// <param name="str" type="String">要转换的字符串</param>
                /// <returns type="Date">日期对象</returns>
                return eval("new " + str.replace(///g, ""));
            }
        }
    }
    
    
    /*************************************** String扩展 ***************************************/
    String.prototype.fill = function (total, char) {
        /// <summary>将使用指定的字符将字符串填充到指定的长度</summary>
        /// <param name="total" type="Number">字符串的总长度</param>
        /// <param name="char" type="String">填充字符</param>
        /// <returns type="String">填充好的字符串</returns>
    
        var fillCount = total - this.length;
        if (fillCount <= 0) {
            return this;
        }
        var text = "";
        for (var i = 0; i < fillCount; i++) {
            text += char;
        }
        return text + this;
    };
    

      

  • 相关阅读:
    NOI Online 2020「Prelude」
    CF704E Iron Man
    luogu P4619 [SDOI2018]旧试题
    luogu P4207 [NOI2005]月下柠檬树
    JOI2020
    luogu P3263 [JLOI2015]有意义的字符串
    p1864
    p1824
    p1836
    p1862
  • 原文地址:https://www.cnblogs.com/lihui1030/p/3328512.html
Copyright © 2011-2022 走看看