The following function takes in two dates and returns the number of days between them: // Calculate the difference of two dates in total days function diffDays(d1, d2) { let numDays = 0; let tv1 = d1.getTime(); // msec since 1970 let tv2 = d2.getTime(); numDays = (tv2 – tv1) / 1000 / 86400; numDays … Continue reading JavaScript: How to calculate the number of days between two dates
Date()
JavaScript: How to compare dates
For an explanation of how to get an ISO date-time string, see this previous article: JavaScript: How to create a formatted date time string Let’s say we have two ISO date-time strings that we pulled from a MySQL database and we want to programmatically compare them to see which date is newer. Let’s set them … Continue reading JavaScript: How to compare dates
JavaScript: How to add and subtract days from ISO date-time strings
For an explanation of what an ISO date-time string is, see this previous article: JavaScript: How to create a formatted date time string Now let’s say we get today’s date and we want to also get dates for one week ago and one week in the future. We can get today’s date in the form … Continue reading JavaScript: How to add and subtract days from ISO date-time strings
JavaScript: How to create a formatted date time string
In JavaScript, to get today’s date and time, all we need to do is use a call to Date() For example: let today = new Date(); This gives us today’s date and the current time, in the form of a Date object that looks like this: ‘Date’ is the key (left), and the value is … Continue reading JavaScript: How to create a formatted date time string