jsは2つの日付の時間差の年数、月数、日数、時間数、分、秒数を計算します.(毎月の日数を考慮して特殊処理を行います.)


jsは2つの日付の時間差の年数、月数、日数、時間数、分数、秒数を計算します.
  • エラーがありましたら、ご指摘をお願いします.
    /**
     *              、  、  、   、   、  
     * DIFFTIME(    ,    ,[  ]),      "y" 、"M"、"d"、"h"、"m"、"s"'
     * console.log(DIFFTIME('2019-6-30 13:20:00', '2020-10-01 11:20:32', 's'))
     */
     
    export const DIFFTIME= function (startTime, endTime, unit) {
    	//        
        function getDays (mouth, year) {
            let days = 30
            if (mouth === 2) {
                days = year % 4 === 0 ? 29 : 28
            } else if (mouth === 1 || mouth === 3 || mouth === 5 || mouth === 7 || mouth === 8 || mouth === 10 || mouth === 12) {
                //    :1,3,5,7,8,10,12  ,   .    31;
                days = 31
            }
            return days
        }
        const start = new Date(startTime)
        const end = new Date(endTime)
        //        
        const diffValue = end - start
        //    
        const startYear = start.getFullYear()
        const endYear = end.getFullYear()
        //    
        const startMouth = start.getMonth() + 1
        const endMouth = end.getMonth() + 1
        //    
        const startDay = start.getDate()
        const endDay = end.getDate()
        //     
        const startHours = start.getHours()
        const endHours = end.getHours()
        //    
        const startMinutes = start.getMinutes()
        const endMinutes = end.getMinutes()
        //    
        const startSeconds = start.getSeconds()
        const endSeconds = end.getSeconds()
        //           
        // console.log('start:', startYear, startMouth, startDay, startHours, startMinutes, startSeconds)
        // console.log('end:', endYear, endMouth, endDay, endHours, endMinutes, endSeconds)
        if (unit === 'y' || unit === 'M') {
            //       
            const diffYear = endYear - startYear
            //        
            const startDays = getDays(startMouth, startYear)
            const endDays = getDays(endMouth, endYear)
            const diffStartMouth = (startDays - (startDay + ((startHours * 60 + startMinutes + startSeconds / 60) / 60 / 24) - 1)) / startDays
            const diffEndMouth = (endDay + ((endHours * 60 + endMinutes + endSeconds / 60) / 60 / 24) - 1) / endDays
            const diffMouth = diffStartMouth + diffEndMouth + (12 - startMouth - 1) + endMouth + (diffYear - 1) * 12
            if (unit === 'y') {
                return Math.floor(diffMouth / 12 * 100) / 100
            } else {
                return diffMouth
            }
        } else if (unit === 'd') {
            const d = parseInt(diffValue / 1000 / 60 / 60 / 24)
            return d
        } else if (unit === 'h') {
            const h = parseInt(diffValue / 1000 / 60 / 60)
            return h
        } else if (unit === 'm') {
            const m = parseInt(diffValue / 1000 / 60)
            return m
        } else if (unit === 's') {
            const s = parseInt(diffValue / 1000)
            return s
        } else {
            console.log('        ')
        }
    }