Javascript Get Difference Between Two Dates In Days

JavaScript

Nicesnippets

678

04-12-2019


Hii Guys,

In this example, I will learn you how to get difference between two dates in days javascript.you can simply get to difference between two dates in days.

Solution:-

function differenceInDays(dt1, dt2)

{

var time =(dt2.getTime() - dt1.getTime());

var days = time / (1000 * 3600 * 24);

return days;

}

Example:-

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="utf-8">

<title>Javascript Get Difference Between Two Dates In Days</title>

</head>

<body>

<script type = "text/javascript">

function differenceInDays(dt1, dt2)

{

var time =(dt2.getTime() - dt1.getTime());

var days = time / (1000 * 3600 * 24);

return days;

}

dt1 = new Date("2019-11-30");

dt2 = new Date("2019-12-03");

var daysDiff = differenceInDays(dt1, dt2)

document.write( "Javascript Get Difference Between Two Dates In Days :- " + daysDiff );

</script>

</body>

</html>

Output:-

Javascript Get Difference Between Two Dates In Days:-3

It will help you.....


Recommended Posts