先端の各種日付操作【コレクションに値する】


前言
現在の処理日付については、成熟してよく使われるライブラリがありますが、例えば(momenntjsdate-fns)、実際の開発においては、ライブラリ全体が必要ではない場合があります.先端開発時の日付時間に対する各種操作を下に整理しました.その中の一部は自分から来ています.また一部は私達の万能なネット友達から来ています.
現在のタイムスタンプを取得
var timestamp = Date.parse(new  Date()); //    
var timestamp = (new Date()).valueOf();  //     
var timestamp = new Date().getTime(); //     
var timestamp = +new Date();
指定タイムスタンプの取得
var timestamp = (new Date(" 2019/10/24 08:00:00")).getTime();
var timestamp = (new Date(" 2019-10-24 08:00:00")).getTime();
現在の時刻の前の日/後の日のタイムスタンプを取得します.
var timestamp = +new Date() - 24*60*60*1000;
var timestamp = +new Date() + 24*60*60*1000;
今日のタイムスタンプ
var timestamp = new Date(new Date().toLocaleDateString()).getTime();
今日の最終時間は23:59:59のタイムスタンプです.
let timestamp = new Date(new Date().toLocaleDateString()).getTime()+24*60*60*1000-1;
現在の時刻のn日後のタイムスタンプを取得します.
/**
 * @param {number} n   
 * @returns {Number}          
 */
function toNextTimes(n){
    let timestamp = +new Date() + n * 86400000;
    return timestamp;
}
今週の初日
/***
 *  @return {*} WeekFirstDay           
 */
function showWeekFirstDay(){
    let Nowdate=new Date();
    let WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000);
    return WeekFirstDay;
}
今週の最後の日
/***
 *  @return {*} WeekLastDay            
 */
function showWeekLastDay(){
    let Nowdate=new Date();
    let WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000);
    let WeekLastDay=new Date((WeekFirstDay/1000+6*86400)*1000);
    return WeekLastDay;
}
今月の初めの日
/***
 *  @return {*} MonthFirstDay           
 */
function showMonthFirstDay(){
    let Nowdate=new Date();
    let MonthFirstDay=new Date(Nowdate.getFullYear(),Nowdate.getMonth());
    return MonthFirstDay;
}
今月最後の日
/***
 *  @return {*} MonthLastDay            
 */
function showMonthLastDay(){
    let Nowdate=new Date();
    let MonthNextFirstDay=new Date(Nowdate.getFullYear(),Nowdate.getMonth()+1);
    let MonthLastDay=new Date(MonthNextFirstDay-86400000);
    return MonthLastDay;
}
日付切り替えタイムスタンプ
/**
 * @param {String} time -      , '2018-8-8','2018,8,8','2018/8/8'
 * @returns {Number}          
 */
function timeToTimestamp (time) {
    let date = new Date(time);
    let timestamp = date.getTime();
    return timestamp;
}
現在の時間を書式設定
/***
 *  @return {string} timeText          
 */
function getdataTimeSec() {
    let time = new Date();
    let weekDay;
    let year = time.getFullYear();
    let month = time.getMonth() + 1;
    let day = time.getDate();
    //     
    let h = time.getHours();
    let m = time.getMinutes();
    let s = time.getSeconds();
    //      10
    h = check(h);
    m = check(m);
    s = check(s);
    let now_day = time.getDay();
    switch (now_day) {
        case 0: {
            weekDay = "   "
        }
            break;
        case 1: {
            weekDay = "   "
        }
            break;
        case 2: {
            weekDay = "   "
        }
            break;
        case 3: {
            weekDay = "   "
        }
            break;
        case 4: {
            weekDay = "   "
        }
            break;
        case 5: {
            weekDay = "   "
        }
            break;
        case 6: {
            weekDay = "   "
        }
            break;
        case 7: {
            weekDay = "   "
        }
            break;
    }
    let timeText = year + " " + month + " " + day + "   " + " " weekDay + " " + h + ":" + m +":" + s;

    return timeText
}
指定されたタイムスタンプの間の時間間隔を返します.
/**
 *  @param {*} startTime         
 *  @param {*} endTime         
 *  @return {string} str        
 */
function getTimeInterval(startTime, endTime) {
    let runTime = parseInt((endTime - startTime) / 1000);
    let year = Math.floor(runTime / 86400 / 365);
    runTime = runTime % (86400 * 365);
    let month = Math.floor(runTime / 86400 / 30);
    runTime = runTime % (86400 * 30);
    let day = Math.floor(runTime / 86400);
    runTime = runTime % 86400;
    let hour = Math.floor(runTime / 3600);
    runTime = runTime % 3600;
    let minute = Math.floor(runTime / 60);
    runTime = runTime % 60;
    let second = runTime;
    let str = '';
    if (year > 0) {
        str = year + ' ';
    }
    if (year <= 0 && month > 0) {
        str = month + ' ';
    }
    if (year <= 0 && month <= 0 && day > 0) {
        str = day + ' ';
    }
    if (year <= 0 && month <= 0 && day <= 0 && hour > 0) {
        str = hour + '  ';
    }
    if (year <= 0 && month <= 0 && day <= 0 && hour <= 0 && minute > 0) {
        str = minute + '  ';
    }
    if (year <= 0 && month <= 0 && day <= 0 && hour <= 0 && minute <= 0 && second > 0) {
        str += second + ' ';
    }
    str += ' ';
    return str;
}
日付をタイプ別に書式設定します.
/**
 * @param {*} date       
 * @param {string} dateType       
 * @return {string} dateText              
 */
function getFormatDate(date, dateType) {
    let dateObj = new Date(date);
    let month = dateObj.getMonth() + 1;
    let strDate = dateObj.getDate();
    let hours = dateObj.getHours();
    let minutes = dateObj.getMinutes();
    let seconds = dateObj.getSeconds();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;

    }
    if (hours >= 0 && hours <= 9) {
        hours = "0" + hours
    }
    if (minutes >= 0 && minutes <= 9) {
        minutes = "0" + minutes
    }
    if (seconds >= 0 && seconds <= 9) {
        seconds = "0" + seconds
    }

    let dateText = dateObj.getFullYear() + ' ' + (dateObj.getMonth() + 1) + ' ' + dateObj.getDate() + ' ';
    if (dateType == "yyyy-mm-dd") {
        dateText = dateObj.getFullYear() + '-' + (dateObj.getMonth() + 1) + '-' + dateObj.getDate();
    }
    if (dateType == "yyyy.mm.dd") {
        dateText = dateObj.getFullYear() + '.' + (dateObj.getMonth() + 1) + '.' + dateObj.getDate();
    }
    if (dateType == "yyyy-mm-dd MM:mm:ss") {
        dateText = dateObj.getFullYear() + '-' + month + '-' + strDate + ' ' + hours + ":" + minutes + ":" + seconds;
    }
    if (dateType == "mm-dd MM:mm:ss") {
        dateText = month + '-' + strDate + ' ' + hours + ":" + minutes + ":" + seconds;
    }
    if (dateType == "yyyy mm dd  MM:mm:ss") {
        dateText = dateObj.getFullYear() + ' ' + month + ' ' + strDate + ' ' + ' ' + hours + ":" + minutes + ":" + seconds;
    }
    return dateText;
 }
うるう年かどうかを判断する
/**
* @param  {number} year       
* @return {boolean}      
*/
function leapYear(year) {
    return !(year % (year % 100 ? 4 : 400));
}
2年の間の閏年を返します.
/**
* @param  {number} start     
* @param  {number} end     
* @return {array}  arr          
*/
function leapYears(start, end) {
    let arr = [];
    for (var i=start; i
時間の書式が有効かどうかを判断します.
/**
*    ,  (10:24:06)
* @param  {string} str         
* @return {boolean}      
*/
function isTime(str) {
    var a = str.match(/^(\d{1,2})(:)?(\d{1,2})\2(\d{1,2})$/);
    if (a == null) { return false; }
    if (a[1] >= 24 || a[3] >= 60 || a[4] >= 60) {
        return false
    }
    return true;
}

/**
*    ,   (2019-10-24)
* @param  {string} str         
* @return {boolean}      
*/
function strDateTime(str){
    var result = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
    if (result == null) return false;
    var d = new Date(result[1], result[3] - 1, result[4]);
    return (d.getFullYear() == result[1] && d.getMonth() + 1 == result[3] && d.getDate() == result[4]);
}

/**
*      ,   (2019-10-24 10:24:06)
* @param  {string} str         
* @return {boolean}      
*/
function strDateTime(str){
    var result = str.match(/^(\d{4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/);
    if (result == null) return false;
    var d = new Date(result[1], result[3] - 1, result[4], result[5], result[6], result[7]);
    return (d.getFullYear() == result[1] && (d.getMonth() + 1) == result[3] && d.getDate() == result[4] && d.getHours() == result[5] && d.getMinutes() == result[6] && d.getSeconds() == result[7]);
}
検証日付のサイズ
/**
*  :"2019-10-24"   "2019-10-25"
* @param  {string} d1       1
* @param  {string} d2       2
* @return {boolean}      
*/
function compareDate(d1, d2) {
    return ((new Date(d1.replace(/-/g, "\/"))) < (new Date(d2.replace(/-/g, "\/"))));
}
日付を確認します.今日ですか?
/**
* @param  {string} val        
* @return {boolean}      
*/
function isToday(val){
    return new Date().toLocaleDateString() == new Date(val).toLocaleDateString();
}
昨日の日付を確認します.
/**
* @param  {string} val        
* @return {boolean}      
*/
function isYesterday(val) {
    var today = new Date();
    var yesterday = new Date(now - 1000 * 60 * 60 * 24);
    var test = new Date(val);
    if (yesterday.getYear() === test.getYear() && yesterday.getMonth() === test.getMonth() && yesterday.getDate() === test.getDate()) {
        return true;
    } else {
        return false;
    }
}
何日後の日付を設定しますか?
/**
* @param  {string} date     
* @param  {number} day      
* @return {string}          
*/
function convertDate (date, day) {
    let tempDate = new Date(date);
    tempDate.setDate(tempDate.getDate()+day);
    let Y = tempDate.getFullYear();
    let M = tempDate.getMonth()+1 < 10 ? '0'+(tempDate.getMonth()+1) : tempDate.getMonth()+1;
    let D = tempDate.getDate() < 10 ? '0'+(tempDate.getDate()) : tempDate.getDate();
    let result = Y + "-" + M + "-" + D
    return result;
}
最後に書く
上の関数が間違っていたり、仕事中にあったりしたら、上に書いてないので、指摘してください.