Created
August 17, 2020 16:20
-
-
Save Singhak/48b0b8293583c9c814e70dabdeb3f819 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let startDate = new Date("2010-05-28"); | |
let endDate = new Date("2020-03-01"); | |
function isDateValid(dateToCheck) { | |
return isNaN(Date.parse(dateToCheck)) ? false : true; | |
} | |
function getMilliSecDiff(startDate, endDate) { | |
let startMilliSec = startDate.getTime(); | |
let endMilliSec = endDate.getTime(); | |
return endMilliSec - startMilliSec; | |
} | |
function getSecondDiff(startDate, endDate) { | |
let milisecsDiff = getMilliSecDiff(startDate, endDate); | |
// 1 sec = 1000ms | |
return milisecsDiff / 1000; | |
} | |
function getMinuteDiff(startDate, endDate) { | |
let secondDiff = getSecondDiff(startDate, endDate); | |
// 1 min = 60sec | |
return ~~(secondDiff / 60); | |
} | |
function getHourDiff(startDate, endDate) { | |
let hourDiff = getMinuteDiff(startDate, endDate); | |
// 1 hour = 60mins | |
return ~~(hourDiff / 60); | |
} | |
function getDayDiff(startDate, endDate) { | |
let dayDiff = getHourDiff(startDate, endDate); | |
// 1 Day = 24hour | |
return ~~(dayDiff / 24); | |
} | |
function getWeekDiff(startDate, endDate) { | |
let weekDiff = getDayDiff(startDate, endDate); | |
// 1 Day = 24hour | |
return ~~(weekDiff / 7); | |
} | |
console.log(getMilliSecDiff(startDate, endDate)); | |
console.log(getSecondDiff(startDate, endDate)); | |
console.log(getMinuteDiff(startDate, endDate)); | |
console.log(getHourDiff(startDate, endDate)); | |
console.log(getWeekDiff(startDate, endDate)); | |
console.log(getDayDiff(startDate, endDate)); | |
// function getYearDiff(fromDate, toDate) {} | |
function getMonthDiff_v1(startDate, endDate) { | |
if (!isDateValid(startDate) && !isDateValid(endDate)) | |
return "Date(s) is/are not valid"; | |
if (endDate < startDate) { | |
return "End Date Date is less than To Start Date"; | |
} | |
let monthsFromYearDiff = | |
(endDate.getFullYear() - startDate.getFullYear()) * 12; | |
let monthsFromMonthDiff = endDate.getMonth() - startDate.getMonth(); | |
let dayDiff = endDate.getDate() - startDate.getDate(); | |
if (dayDiff < 0) { | |
// means fromDate is not a complete month | |
return monthsFromYearDiff + monthsFromMonthDiff - 1; | |
} | |
return monthsFromYearDiff + monthsFromMonthDiff; | |
} | |
console.log(getMonthDiff_v1(startDate, endDate)); | |
function getMonthDiff_v2(startDate, endDate) { | |
var diff = (endDate.getTime() - startDate.getTime()) / 1000; | |
diff /= (60 * 60 * 24 * 365) / 12; | |
return Math.abs(Math.round(diff)); | |
} | |
console.log(getMonthDiff_v2(startDate, endDate)); | |
function getMonthDiff_v3(startDate, endDate) { | |
let diff = (endDate.getTime() - startDate.getTime()) / 1000; | |
// 1 month = 4 week | |
// 1 week = 7 days | |
// 1 day= (60 * 60 * 24) hours | |
diff /= 60 * 60 * 24 * 7 * 4; | |
return Math.abs(Math.floor(diff)); | |
} | |
console.log(getMonthDiff_v3(startDate, endDate)); | |
function getMonthDayDiff(startDate, endDate) { | |
if (!isDateValid(startDate) && !isDateValid(endDate)) | |
return "Date(s) is/are not valid"; | |
if (endDate < startDate) { | |
return "End Date Date is less than To Start Date"; | |
} | |
let monthsFromYearDiff = | |
(endDate.getFullYear() - startDate.getFullYear()) * 12; | |
let monthsFromMonthDiff = endDate.getMonth() - startDate.getMonth(); | |
let dayDiff = endDate.getDate() - startDate.getDate(); | |
if (dayDiff < 0) { | |
// means fromDate is not a complete month | |
var dtmp = new Date(startDate.getFullYear(), startDate.getMonth() + 1, 0); | |
var numDays = dtmp.getDate(); | |
dayDiff += numDays; | |
let month = monthsFromYearDiff + monthsFromMonthDiff - 1; | |
return { month, dayDiff }; | |
} | |
let month = monthsFromYearDiff + monthsFromMonthDiff; | |
return { month, dayDiff }; | |
} | |
console.log(getMonthDayDiff(startDate, endDate)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment