• javascript Date.prototype


    Date.prototype.format = function(format){
    var o = {
    "M+" : this.getMonth()+1, //month
    "d+" : this.getDate(), //day
    "h+" : this.getHours(), //hour
    "m+" : this.getMinutes(), //minute
    "s+" : this.getSeconds(), //second
    "q+" : Math.floor((this.getMonth()+3)/3), //quarter
    "S" : this.getMilliseconds() //millisecond
    };
    if(/(y+)/.test(format)) {
    format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
    }
    for(var k in o) {
    if(new RegExp("("+ k +")").test(format)) {
    format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
    }
    }
    return format;
    };

    // `getDayOfYear` returns the day number for the year
    Date.prototype.getDayOfYear = function () {
    var fd = new Date(this.getFullYear(), 0, 0);
    var sd = new Date(this.getFullYear(), this.getMonth(), this.getDate());
    return Math.ceil((sd - fd) / 86400000);
    };

    // `getWeekOfYear` returns the week number for the year
    Date.prototype.getWeekOfYear = function () {
    var ys = new Date(this.getFullYear(), 0, 1);
    var sd = new Date(this.getFullYear(), this.getMonth(), this.getDate());
    if (ys.getDay() > 3) {
    ys = new Date(sd.getFullYear(), 0, (7 - ys.getDay()));
    }
    var daysCount = sd.getDayOfYear() - ys.getDayOfYear();
    return Math.ceil(daysCount / 7);

    };

    // `getDaysInMonth` returns the number of days in a month
    Date.prototype.getDaysInMonth = function () {
    return 32 - new Date(this.getFullYear(), this.getMonth(), 32).getDate();
    };

    // `hasWeek` returns `true` if the date resides on a week boundary
    // **????????????????? Don't know if this is true**
    Date.prototype.hasWeek = function () {
    var df = new Date(this.valueOf());
    df.setDate(df.getDate() - df.getDay());
    var dt = new Date(this.valueOf());
    dt.setDate(dt.getDate() + (6 - dt.getDay()));

    if (df.getMonth() === dt.getMonth()) {
    return true;
    } else {
    return (df.getMonth() === this.getMonth() && dt.getDate() < 4) || (df.getMonth() !== this.getMonth() && dt.getDate() >= 4);
    }
    };

    // `getDayForWeek` returns the Date object for the starting date of
    // the week # for the year
    Date.prototype.getDayForWeek = function () {
    var df = new Date(this.valueOf());
    df.setDate(df.getDate() - df.getDay());
    var dt = new Date(this.valueOf());
    dt.setDate(dt.getDate() + (6 - dt.getDay()));
    if ((df.getMonth() === dt.getMonth()) || (df.getMonth() !== dt.getMonth() && dt.getDate() >= 4)) {
    return new Date(dt.setDate(dt.getDate() - 3));
    } else {
    return new Date(df.setDate(df.getDate() + 3));
    }
    };

    // `getWeekId` returns a string in the form of 'dh-YYYY-WW', where WW is
    // the week # for the year.
    // It is used to add an id to the week divs
    Date.prototype.getWeekId = function () {
    var y = this.getFullYear();
    var w = this.getDayForWeek().getWeekOfYear();
    var m = this.getMonth();
    if (m === 11 && w === 1) {
    y++;
    }
    return 'dh-' + y + "-" + w;
    };

  • 相关阅读:
    【转】QT创建子对话框的方法
    IplImage转为Mat的方法
    浅谈Android选项卡(二)
    浅谈Android选项卡(一)
    Android来电、去电监听
    文件加密
    Java实现文件重命名
    使用单个httpclient实例请求数据。
    获取Android状态栏的高度
    [置顶] 微软翻译接口
  • 原文地址:https://www.cnblogs.com/yangbt/p/3835903.html
Copyright © 2020-2023  润新知