xFactorSchool.com(Beta)
How to Do Examples
Python Examples
HTML Examples
Code Examples
C Code Examples
C++ Code Examples
C# Code Examples
Java Code Examples
Python Code Examples
HTML Code Examples
CSS Code Examples
JavaScript Code Examples
Online Editor
C Code Editor
C++ Code Editor
C# Code Editor
Java Code Editor
Python Code Editor
HTML Code Editor
CSS Code Editor
JavaScript Code Editor
Practice how to find difference between two dates in JavaScript
JavaScript Online Editor
Run Code
More JavaScript Sample Codes
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>How to find difference between two dates in JavaScript</title> <!--Script tag is added to write JavaScript code inside it--> <script> var previousDate = new Date(2023, 10, 11); //set a previous date and set var currentDate = new Date(); //get the current date and time var timedifferenceMS = currentDate - previousDate; //Time difference in milliseconds var milliSeconds = timedifferenceMS % 1000; //get milliseconds using % var seconds = (timedifferenceMS / 1000) % 60; //get seconds using % var minutes = (timedifferenceMS / (1000 * 60)) % 60; //get minutes using % var hours = (timedifferenceMS / (1000 * 60 * 60))% 24; //get hours using % var days = (timedifferenceMS / (1000 * 60 * 60 * 24)); function myFunction() { //Math.floor() method is used to get floor value document.getElementById("outPut").innerHTML = "The difference is: " + Math.floor(days) + " days " + Math.floor(hours) + " hours " + Math.floor(minutes) + " minutes "+ Math.floor(seconds) + " seconds " + Math.floor(milliSeconds) + " millisoconds"; }; </script> </head> <body> <h1>How to Find Difference Between Two Dates in JavaScript?</h1> <button onclick="myFunction()">Click here to see result</button> <p id="outPut"></p> </body> </html>