To get the difference in minutes between two dates in JavaScript, we can create two Date
objects, subtract one from the other, and then divide the result by the number of milliseconds in a minute (60,000).
Here's an example:
index.tsx242 chars8 lines
In the above code snippet, we create two Date
objects for May 1st, 2022, at 12:00 PM and 12:30 PM respectively. We then subtract the first date (date1
) from the second date (date2
) to get the difference between them in milliseconds. We convert this difference to minutes by dividing by 60,000 (60 seconds * 1000 milliseconds), and then round down the result using Math.floor()
to get a whole number of minutes. Finally, we log the result to the console.
gistlibby LogSnag