文章出處

for Date

github: https://github.com/laixiangran/commonJS/blob/master/src/forDate.js

代碼

/**
 * Created by laixiangran on 2016/1/24
 * homepage:http://www.cnblogs.com/laixiangran/
 * for Date
 */

(function(undefined) {

    var com = window.COM = window.COM || {};

    com.$DE = {

        // 求當前日期與傳入的日期相隔多少天
        getDateInterval: function(date) {
            var d = new Date(date);
            if (d == "Invalid Date") {
                throw "Invalid Date";
            }else {
                // Math.abs 絕對值
                return Math.abs(this*1-d*1)/60/60/1000/24;
            }
        },

        // 求當前日期所在月的第一天
        getFirstDateInMonth: function(date) {
            return new Date(date.getFullYear(), date.getMonth(), 1);
        },

        // 求當前日期所在月的最后一天
        getLastDateInMonth: function(date) {
            return new Date(date.getFullYear(), date.getMonth()+1, 0);
        },

        // 求當前日期所在季度的第一天
        getFirstDateInQuarter: function(date) {
            return new Date(date.getFullYear(), Math.floor(date.getMonth()/3)*3, 1);
        },

        // 判斷是否為閏年
        isLeapYear: function(date) {
            return new Date(date.getFullYear(), 2, 0).getDate() == 29;
        },

        // 求某年某月的天數
        daysInMonth: function(year, month) {
            var d = new Date();
            d.setFullYear(year, (month == 12) ? 1 : month, 0);
            return d.getDate();
        }
    };
}());

參考

http://www.cnblogs.com/rubylouvre/archive/2010/09/16/1827784.html


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜

    大師兄 發表在 痞客邦 留言(0) 人氣()