打造自己的js库(date篇)

                                                  打造自己的js库(date篇)

项目中经常需要一些对时间进行格式化的需求,比如时间戳按指定格式转为"年-月-日"或者"年/月/日"的格式,这一篇就是提供一些类似new Date().Format这样的一些方法。

    namespace('gu.date');

    extend(gu.date,{
    /**
    *获取日期,默认日期为今天,默认分隔符为'-'
    * @param time
    * @returns {string}
    * @date 2017-11-21
    */
    toDateString:function (time) {
    var time = time ? time : new Date(),
    m = time.getMonth(),
    d = time.getDay(),
    y = time.getFullYear(),
    separator = arguments[1] ? arguments[1] : '-';

    return y + separator + (m.toString().length < 2 ? '0' + m : m) +separator +(d.toString().length < 2 ? '0' + d : d);
    },
    /**
    * 获取日期以及时间,默认日期为今天,默认分隔符为'-'
    * @param time
    * @returns {string}
    * @date 2017-11-21
    */
    toDateTimeString:function (time) {
    var time = time ? time : new Date(),
    h = time.getHours(),
    i = time.getMinutes(),
    s = time.getSeconds();

    return gu.date.toDateString.apply(this,arguments) + " " + (h.toString().length < 2 ? '0' + h : h)+ ":" + (i.toString().length < 2 ? '0' + i : i)+ ":" + (s.toString().length < 2 ? '0' + s : s);
    }
    });

猜你喜欢

转载自blog.csdn.net/qq_39771254/article/details/81083398