天天看點

JS時間操作

/**
 * 判斷年份是否為潤年
 *
 * @param {Number} year
 */
function isLeapYear(year) {
    return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}
/**
 * 擷取某一年份的某一月份的天數
 *
 * @param {Number} year
 * @param {Number} month
 */
function getMonthDays(year, month) {
    return [31, null, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month] || (isLeapYear(year) ? 29 : 28);
}  /**
 * 擷取某年的某天是第幾周
 * @param {Number} y
 * @param {Number} m
 * @param {Number} d
 * @returns {Number}
 */
function getWeekNumber(now) {
    year = now.getFullYear(),
    month = now.getMonth(),
    days = now.getDate();
    //那一天是那一年中的第多少天
    for (var i = 0; i < month; i++) {
        days += getMonthDays(year, i);
    }

    //那一年第一天是星期幾
    var yearFirstDay = new Date(year, 0, 1).getDay() || 7;

    var week = null;
    if (yearFirstDay == 1) {
        week = Math.ceil(days / yearFirstDay);
    } else {
        days -= (7 - yearFirstDay + 1);
        week = Math.ceil(days / 7) + 1;
    }

    return week;
}

//一個月有多少天
function getCountDays(curDate) {

    /* 擷取目前月份 */
    var curMonth = curDate.getMonth();
    /*  生成實際的月份: 由于curMonth會比實際月份小1, 故需加1 */
    curDate.setMonth(curMonth + 1);
    /* 将日期設定為0, 這裡為什麼要這樣設定, 我不知道原因, 這是從網上學來的 */
    curDate.setDate(0);
    /* 傳回當月的天數 */
    return curDate.getDate();
}

function getWeekStart(curDate) {

    var curMonth = curDate.getMonth();
    curDate.setDate(curDate.getDate() + 1 - curDate.getDay());//
    return curDate;
}
function getWeekEnd(curDate) {

    curDate.setDate(curDate.getDate() + 7 - curDate.getDay());
    return curDate;
}      

NetAnalyzer下載下傳位址

NetAnalzyer交流群:39753670 (PS 隻提供交流平台,群主基本不說話^_^)

[轉載請保留作者資訊  作者:馮天文 ]