Get the relative timestamp difference between dates in JavaScript Last Updated : 30 Dec, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Given the 2 JavaScript dates and the job is to get the relative time difference between them(eg.. 2 hours ago, 2.5 days ago, etc.) Here 2 approaches are discussed with the help of javaScript. Approach 1: Get the prevDate and currDate in a variable using Date() object .Calculate the milliseconds in Minute, Hour, Day, Month and in an Year.Calculate the milliseconds difference between the prevDate and currDate.Compare these milliseconds with the milliseconds in Minute, Hour, Day, Month and in an year in this order.If the milliseconds are less than any of them, then calculate the respective Minutes, Hours, Days, Months and Years. Example 1: This example implements the above approach. html <body> <h1 style="color:green;"> GeeksforGeeks </h1> <p id="GFG_UP"> </p> <button onclick="GFG_Fun();"> click here </button> <p id="GFG_DOWN"> </p> <script> var up = document.getElementById('GFG_UP'); var down = document.getElementById('GFG_DOWN'); var prevDate = new Date(); prevDate.setDate(prevDate.getDate() - 6); up.innerHTML = "Click on the button to get relative time difference between dates." + "<br><br>Older Date : " + prevDate; function timeDiff(curr, prev) { var ms_Min = 60 * 1000; // milliseconds in Minute var ms_Hour = ms_Min * 60; // milliseconds in Hour var ms_Day = ms_Hour * 24; // milliseconds in day var ms_Mon = ms_Day * 30; // milliseconds in Month var ms_Yr = ms_Day * 365; // milliseconds in Year var diff = curr - prev; //difference between dates. // If the diff is less then milliseconds in a minute if (diff < ms_Min) { return Math.round(diff / 1000) + ' seconds ago'; // If the diff is less then milliseconds in a Hour } else if (diff < ms_Hour) { return Math.round(diff / ms_Min) + ' minutes ago'; // If the diff is less then milliseconds in a day } else if (diff < ms_Day) { return Math.round(diff / ms_Hour) + ' hours ago'; // If the diff is less then milliseconds in a Month } else if (diff < ms_Mon) { return 'Around ' + Math.round(diff / ms_Day) + ' days ago'; // If the diff is less then milliseconds in a year } else if (diff < ms_Yr) { return 'Around ' + Math.round(diff / ms_Mon) + ' months ago'; } else { return 'Around ' + Math.round(diff / ms_Yr) + ' years ago'; } } function GFG_Fun() { var diff = timeDiff(new Date(), prevDate) document.getElementById("GFG_DOWN").innerHTML = diff; } </script> </body> Output: Approach 2: Get the prevDate and currDate in a variable.Calculate the seconds, Minutes, Hours and Days difference between the dates.Compare these parameters with the 60 seconds, 60 Minutes, 24 Hours, Day in this order.If any of those condition satisfies then return the respective parameter. Example 2: This example implements the above approach. html <body> <h1 style="color:green;"> GeeksforGeeks </h1> <p id="GFG_UP"> </p> <button onclick="GFG_Fun()"> click here </button> <p id="GFG_DOWN"> </p> <script> var up = document.getElementById('GFG_UP'); var down = document.getElementById('GFG_DOWN'); var prevDate = new Date(); prevDate.setDate(prevDate.getDate() - 6); prevDate.setHours(prevDate.getHours() - 6); up.innerHTML = "Click on the button to get relative time " + "difference between dates.<br><br>Older Date : " + prevDate; function conversion(ms) { // Calculating Seconds var sec = (ms / 1000).toFixed(1); // Calculating Minutes var min = (ms / (1000 * 60)).toFixed(1); // Calculating hours var hrs = (ms / (1000 * 60 * 60)).toFixed(1); // Calculating days var days = (ms / (1000 * 60 * 60 * 24)).toFixed(1); if (sec < 60) { return sec + " Sec"; } else if (min < 60) { return min + " Min"; } else if (hrs < 24) { return hrs + " Hrs"; } else { return days + " Days" } } function GFG_Fun() { var date = new Date(); var diff = conversion(date.getTime() - prevDate.getTime()) document.getElementById("GFG_DOWN").innerHTML = diff; } </script> </body> Output: Comment More infoAdvertise with us Next Article Get the relative timestamp difference between dates in JavaScript P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies javascript-date JavaScript-Questions Similar Reads How to Get Hours Difference Between Two Dates in Moment Js? Moment.js library has a set of powerful functions for date and time manipulation, including calculating differences between dates. To get the hours difference between two dates, you can use various methods provided by Moment.js. Run the below command before running the code in your local system:npm 2 min read Difference Between new Date() and Date.now() in JavaScript In JavaScript, handling dates and times is essential for various applications from displaying the current date to performing complex calculations involving time differences. Two commonly used methods for working with the dates are new Date() and Date.now(). Despite dealing with the time they serve d 2 min read How to Get time Difference Between Datetimes in MomentJS? MomentJS is a popular JavaScript library used for parsing, validating, manipulating, and formatting dates. One common requirement when working with dates is calculating the difference between two date times. Run the below command before running the code in your local system:npm i momentThese are the 2 min read How to get Difference between two Dates in days using jQuery ? In this article, we will learn how to find the difference between the 2 dates in days using jQuery. Here, we will take the input for the dates using a jquery datepicker.Considering the given below 2 input Dates, we need to calculate the difference between them which will be returned in a number of d 3 min read JavaScript Get the start and end of the day in UTC Given a date, and the task is to determine the start and end of the day of the date using JavaScript. We're going to discuss a few methods. These are: JavaScript setHours() Method: This method sets the hour of a date object. This method can be used to set the minutes, seconds, and milliseconds. Synt 3 min read Like