码迷,mamicode.com
首页 > 编程语言 > 详细

tinydate.js[v0.1]关于Javascript Date的工具

时间:2018-06-14 21:08:08      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:需求   source   bst   字符   操作   获得   typeof   turn   ring   

编写初衷
JavaScript的Date对象所提供的方法无法满足生产的需要,比如他的Month,他的日期加减,比如我要得到1993年1月1日到现在有多少天,比如我想知道我在这个世界上活了多少个小时,那么都需要“换算”。先得到时间戳,然后在换算~~~

  • 其实只是将常用的操作,写成了js,代码复用减轻工作量。

它的功能

  • TimeStamp
    直接获取某个时间戳、时间对象、时间字符串的年月日时分秒毫秒,完整的年月日时分秒毫秒。
  • 计算时间差
    调用时间差函数会计算两个时间的差值,返回值是 TimeStamp对象(自定义的)
  • 日期格式化
    调用格式化函数可以对日期进行格式化
  • 添加年、月、日、时、分、秒
    将Date原有的setYear...等函数进行拓展,时常会有一种需求去添加某个类型的时间,但是~经常要我们写成 date.setYear(date.getFullYear()+year);非常令人讨厌的一段,封装后只需要date.addYear(year);即可。

代码

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), //季度 
        "f+": 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;
}
//当前完整时间
Date.prototype.$nowDate = new Date().format("yyyy-MM-dd HH:mm:ss.ffff");
//获取自定义格式的当前时间
Date.prototype.$now = function (fmt) {
    return new Date().format(fmt);
}
//计算时间差
Date.prototype.diff = function (sDate, eDate) {
    if (eDate == undefined || eDate == null)
        eDate = new Date();
    var stime = sDate.getTime();
    var etime = eDate.getTime();

    var diffTime = etime - stime;

    var timeSpan = new TimeSpan(diffTime);
    return timeSpan;
}
//添加年
Date.prototype.addYear = function (number) {
    this.setFullYear(this.getFullYear() + number);
}
//添加月
Date.prototype.addMonth = function (number){
    this.setMonth(this.getMonth()+number);
}
//添加日
Date.prototype.addDate = function (number){
    this.setDate(this.getDate()+number);
}
//添加小时
Date.prototype.addHours = function (number){
    this.setHours(this.getHours()+number);
}
//添加分
Date.prototype.addMinutes = function (number){
    this.setMinutes(this.getMinutes()+number);
}
//添加秒
Date.prototype.addSeconds = function (number){
    this.setSeconds(this.getSeconds()+number);
}
//添加毫秒
Date.prototype.addMilliseconds = function (number){
    this.setMilliseconds(this.getMilliseconds()+number);
}

//获得一年中第一天的日期
Date.prototype.getTheFirstDateOfTheYear = function (date) {
    var year, month=0, day=1;
    if (date == undefined || date == null) {
        year = this.getFullYear();
    }
    else {
        year = date.getFullYear();
    }

    return new Date(year, month, day);
}
//获得一年中最后一天的日期
Date.prototype.getTheLastDateOfTheYear = function (date) {
    var year, month = 11, day = 31;
    if (date == undefined || date == null) {
        year = this.getFullYear();
    }
    else {
        year = date.getFullYear();
    }

    return new Date(year, month, day);
}
//格式化当前日期 为 时限对象
Date.prototype.timeSpan = function () {
    return new TimeSpan(this);
}
//时限对象
function TimeSpan() {
    var o = new Object();
    o.year = 0;//年
    o.month = 0;//月
    o.day = 0;//日
    o.hours = 0;//时
    o.minutes = 0;//分
    o.seconds = 0;//秒
    o.milliseconds = 0;//毫秒
    o.totalYear = 0.00;//从时间原点的年
    o.totalMonth = 0.00;//从时间原点到现在的月
    o.totalDay = 0.00;//从时间原点到现在的天
    o.totalHours = 0.00;//从时间原点到现在的小时
    o.totalMinutes = 0.00;//从时间原点到现在的分
    o.totalSeconds = 0.00;//从时间原点到现在的秒
    o.totalMilliseconds = 0.00;//从时间原点到现在的毫秒
    //初始化对象
    o.init = function (timestamp) {
        var odate = new Date(timestamp);
        o.year = odate.getFullYear();
        o.month = odate.getMonth() + 1;
        o.day = odate.getDate();
        o.hours = odate.getHours();
        o.minutes = odate.getMinutes();
        o.seconds = odate.getSeconds();
        o.milliseconds = odate.getMilliseconds();

        o.totalMilliseconds = timestamp;
        o.totalSeconds = (timestamp / 1000).toFixed(2);
        o.totalMinutes = (timestamp / 1000 / 60).toFixed(2);
        o.totalHours = (timestamp / 1000 / 60 / 60).toFixed(2);
        o.totalDay = (timestamp / 1000 / 60 / 60 / 24).toFixed(2);
        o.totalMonth = o.year * 12;
        o.totalYear = o.year;
    }
    //无参则返回空对象
    if (arguments.length == 0) {

    }
    else if (typeof (arguments[0]) === "string") {
        o.init(new Date(arguments[0]));
    }
    else if (typeof (arguments[0]) === "number") {
        o.init(arguments[0]);
    } else if (typeof(arguments[0]) === "object") {
        o.init(arguments[0]);
    }
    return o;
}

tinydate.js[v0.1]关于Javascript Date的工具

标签:需求   source   bst   字符   操作   获得   typeof   turn   ring   

原文地址:https://www.cnblogs.com/nozer1993/p/9184280.html

(0)
(1)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!