js$とDateオブジェクト拡張timeメソッド

13158 ワード

一.拡張
1.$拡張
$.extend({}); // jQuery        ,           。

2.Dateオブジェクト拡張
Date.prototype = {}

二.timeの具体的な方法
1.ビット時間に0接頭辞を付ける
addZero: function(temp) { 
    return temp < 10 ? "0" + temp : temp;
}

2.タイムフォーマットブラウザ互換性
getTimeStap: function(val) {
  return isNaN(val) ? Date.parse(val.replace(new RegExp(/-/gm), "/")) : val
}

3.フォーマット時間
YYYY-MM-DD
getDateformat: function(val, mark = "-") {
    if (val) {
        var dd = new Date($.getTimeStap(val))
        var y = dd.getFullYear();
        var m = $.addZero(dd.getMonth() + 1);
        var d = $.addZero(dd.getDate());
        return y + mark + m + mark + d;
    } else {
        return "";
    }
}

YYYY-MM-DD hh:mm:ss
getDateformatT: function(val, mark = "-") {
   if (val) {
        var dd = new Date($.getTimeStap(val))
        var y = dd.getFullYear();
        var m = $.addZero(dd.getMonth() + 1);
        var d = $.addZero(dd.getDate());
        var hours = $.addZero(dd.getHours());
        var minutes = $.addZero(dd.getMinutes());
        var seconds = $.addZero(dd.getSeconds());
        return y + mark + m + mark + d + " " + hours + ":" + minutes + ":" + seconds;;
    } else {
        return "";
    }
}

4.今日の前後数日の時間を取得
YYYY-MM-DD
getBeforeDay: function(dayNum = 0, mark = "-") { 
    var dd = new Date();
    dd.setDate(dd.getDate() + dayNum); 
    var y = dd.getFullYear();
    var m = $.addZero(dd.getMonth() + 1); 
    var d = $.addZero(dd.getDate());
    return y + mark + m + mark + d;
}

YYYY-MM-DD hh:mm:ss
getBeforeDayT: function(dayNum = 0, mark = "-") { 
    var dd = new Date();
    dd.setDate(dd.getDate() + dayNum); 
    var y = dd.getFullYear();
    var m = $.addZero(dd.getMonth() + 1);
    var d = $.addZero(dd.getDate());
    var hours = $.addZero(dd.getHours());
    var minutes = $.addZero(dd.getMinutes());
    var seconds = $.addZero(dd.getSeconds());
    return y + mark + m + mark + d + " " + hours + ":" + minutes + ":" + seconds;
}

5.今月前後数ヶ月の取得
 getBeforeMonth: function(monthNum = 0, mark = "-") {
    var dd = new Date();
    dd.setMonth(dd.getMonth() + monthNum);
    var y = dd.getFullYear();
    var m = dd.getMonth() + 1;
    return y + mark + $.addZero(m)
}

6.取得時間差
日数差
getDaySub: function(startVal, endVal) {
    return ($.getTimeStap(endVal) - $.getTimeStap(startVal)) / 86400000 + 1 + " "
}

月次日数差
getMonthSub: function(startVal, endVal) {
    var beforeTime = new Date($.getTimeStap(startVal))
    var lastTime = new Date($.getTimeStap(endVal))
    var y = beforeTime.getFullYear()
    var y1 = lastTime.getFullYear()
    var m = beforeTime.getMonth() + 1
    var m1 = lastTime.getMonth() + 1
    var d = beforeTime.getDate()
    var d1 = lastTime.getDate()
    var tm = 0;
    var td = 0;
    if (d < d1 + 1) {
        if (d == 1 && d1 == new Date(y1, m1, 0).getDate()) {
            tm = (y1 - y - 1) * 12 + (m1 + 12 - m) + 1
        } else {
            tm = (y1 - y - 1) * 12 + (m1 + 12 - m)
            td = d1 - d + 1
        }
    } else if (d > d1 + 1) {
        if (m1 > m) {
            tm = (y1 - y) * 12 + (m1 - m - 1)
            td = (d1 - d + 1 + new Date(y1, m1 - 1, 0).getDate())
        } else {
            tm = (y1 - y - 1) * 12 + (m1 - m - 1 + 12)
            if (m1 == 1) {
                td = (d1 - d + 1 + new Date(y1 - 1, 12, 0).getDate())
            } else {
                td = (d1 - d + 1 + new Date(y1, m1 - 1, 0).getDate())
            }
        }
    } else {
        if (m1 == m) {
            tm = (y1 - y) * 12
        } else {
            tm = (y1 - y - 1) * 12 + (m1 - m + 12)
        }
    }
    return { "m": tm, "d": td }
}

7.今月の初日と最終日の日付を取得
 getMonthDay: function(val) {
    var dd = new Date($.getTimeStap(val))
    var y = dd.getFullYear();
    var m = $.addZero(dd.getMonth() + 1);
    var d = new Date(y, m, 0);
    return {
        firstDay: y + '-' + m + '-01',
        lastDay: y + '-' + m + '-' + d.getDate()
    }
}

8.今月の第1日曜日を取得
getMonthWeek: function(val) {
    var dd = new Date($.getTimeStap(val));
    var y = dd.getFullYear();
    var m = dd.getMonth();
    var d = new Date(y, m, 1);
    var week = d.getDay();
    return week
}

9.ある日の年、月、日、曜日および今月の日数を取得
getDay: function(val = new Date()) { 
    var dd = new Date($.getTimeStap(val));
    var y = dd.getFullYear();
    var m = dd.getMonth() + 1;
    var d = dd.getDate();
    var ds = new Date(y, m, 0);
    return {
        ym: y + "-" + $.addZero(m),
        d: $.addZero(d),
        y: y,
        m: m,
        ds: ds.getDate(),
        week: dd.getDay()
    }
}