If you are looking for a JavaScript function to get the difference between the two dates in minutes then you have come to the right place.Here is the simple Javascript code
function GetdiffinMinute()
{
var d1 = new Date("12/04/2020")
var d2 = new Date("11/04/2020")
var diff = (d1.getTime() - d2.getTime()) / 60000;
var mintdiff = Math.abs(Math.round(diff));
return mintdiff
}
Subtracting two Date objects gives you the result in milliseconds .Now we want result in minutesĀ so that Dividing the result by 1000 gives you the number of seconds. And then Dividing that value by 60 gives you the number of minutes. So you can say that divide the value by 60000 gives you difference in minutes.
Using Above Concept you can also write a generic javascript function which will return the difference between two dates in minute,hours ,days,milliseconds.
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<button onclick="GetdiffinMinute()" type="button">Click</button>
<script type="text/javascript">
function GetdiffinMinute()
{
var todaydate = new Date();
var Newyearday = new Date("2020-01-01");
var diffMs = (todaydate - Newyearday); //diffrence in milliseconds between now & Christmas
var diffDays = Math.floor(diffMs / 86400000); //diffrence in days
var diffHrs = Math.floor((diffMs % 86400000) / 3600000); //diffrence in hours
var diffMins = Math.round(((diffMs % 86400000) % 3600000) / 60000); //diffrence in minutes
alert(diffDays + " days, " + diffHrs + " hours, " + diffMins + " minutes until Christmas 2009 =)");
}
</script>
</body>
</html>
The post [Solved]-Difference between two dates in minutes Javascript appeared first on Software Development | Programming Tutorials.
Read More Articles
- LINQ group by datetime and sum | Linq get sum of data group by date
- [Simple Way]-JavaScript Edit table row Using Popup
- Crud operation in JavaScript using local storage | Crud operation using local storage with es6
- [Simple Way]-Add/Delete table rows dynamically using JavaScript
- How to edit/delete selected row from HTML table using JavaScript
- [Simple Way]-CRUD operations using JSON in JavaScript
- Convert JSON data to HTML table using JavaScript
- Find object by id in an array of JavaScript objects
- [Solved]-Cookie loses value when page is changed in MVC
- Display JSON data in HTML table using JavaScript dynamically
- Add edit and delete data in an HTML table using JavaScript
- [Solved]-How to call rest api from Excel macros vba and Parse Json
- Difference between Flutter and React Native | React Native vs Flutter
- What is Difference between where and having clauses in SQL server
- Difference Between EXCEPT and NOT IN
- What is difference between subquery and correlated subquery
- [Solved]-How to Upload pdf file using jquery MVC?
- [Solved]-Uploading both data and files in FormData using Ajax MVC
- [Solved]-How to format Date Object in MM/DD/YYYY HH:MM:SS format using JavaScript ?
- [Solved]-Difference between two dates in minutes Javascript
- Differences Between Derived Table & CTE,Views,Temp Table and Temp Variable
- [Solved]-How to get days,minute, hours between two dates in JavaScript?
- [Solved]-How to add minutes in Date Object using javascript
- [Solved]-Download pdf file from link and save in local file folder in Asp .Net
- [Solved]-How Upload and Download PDF file Database in ASP.Net?
- [Solved]-Set,Delete and Read cookie value by name in JavaScript
- Difference between view and indexed view or materialized
- [Solved]-Delete Files older than X Months,Days,Minute in a Directory- C# .NET
- [Solved]-LEFT OUTER JOIN in LINQ With Where clause in C#
- Sql Server- Difference between unique and non unique index
- [Solved]-Convert Base64 To Image And Save in folder and Display it- C#
- [Solved]-Convert an Image Url Into Base64 Data URL Using JavaScript With CORS
- [Solved]-How to Overlay Two Images in .NET-C#
- [Solved]-FFMPEG Converted Mp4 Video Not Working In HTML5-C#
- Client-side Data Compression and Decompression with JavaScript
- Difference between Inner Join ,Left Join and Full Join-Sql
- How to find second or Nth maximum salary from salary table
- [Solved]-How To Update a Table using JOIN in SQL Server?
- [Solved]-Best Sql server query with pagination and count
- [Solved]-Find all duplicate rows based on one or two columns