How to calculate javascript difference between two dates in days


How to calculate javascript difference between two dates in days

In this tutorial, we will learn to calculate the javascript difference between two dates in days. It is often required for web development purposes such as doing calculations on a web page on the fly.

For example:- We have created two date input fields in an HTML file as shown on the image above and the no of days will be shown in the days’ input field.

Also read, Dropdown search box jQuery example with select2.min.js

Illustration of the example to find Javascript difference between two dates

  • Declare the date inputs with the id attribute to get the date value from the input field.
  • Use the javascript onchange event as a function to get the result while changing the mouse from one field to another field input.
  • First we will store the date values in different variables using the document.getElementById() function.
  • Also, we will wrap the date values using the new Date() function to create a date object.
  • Next, we will get the time difference between two dates using the getTime() function in javascript.
  • Finally, we will divide the time difference by the no of milliseconds in a day i.e. (1000360024) in order to get the javascript difference between two dates in days.

<html>
<head>
<title>ajax example</title>
<link rel="stylesheet" href="bootstrap.css" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="bootstrap-theme.css" crossorigin="anonymous">
<style>
.container{
	width:70%;
	height:30%;
	padding:20px;
}
</style>
</head>
<body>
	<div class="container">
		<h3 align="center">Calculate Javascript Difference between Two Dates in Days</h3>
		<br/><br/><br/>
		  <div class="form-group row">
		        <label class="control-label col-sm-2">Start Date:</label>
			    <div class="col-sm-2">
			      <input type="date" id="start_date">
			    </div>
			    <label class="control-label col-sm-2">End Date:</label>
			    <div class="col-sm-2">
				     <input type="date" id="end_date" onchange="getDays()">
				</div>
				<label class="control-label col-sm-2">Days:</label>
			    <div class="col-sm-2">
			      <input type="text" id="days">
			    </div>
			    
		  </div>
		<br/><br>
	
</form>
</div>
<script src="jquery-3.2.1.min.js"></script>
<script src="bootstrap.min.js"></script>
<script>

//get the days between two dates

  function getDays(){

  	var start_date = new Date(document.getElementById('start_date').value);
  	var end_date = new Date(document.getElementById('end_date').value);
  	//Here we will use getTime() function to get the time difference
  	var time_difference = end_date.getTime() - start_date.getTime();
  	//Here we will divide the above time difference by the no of miliseconds in a day
  	var days_difference = time_difference / (1000*3600*24);
  	//alert(days);
  	document.getElementById('days').value = days_difference;
  }
</script>
</body>
</html>

Download the bootstrap CSS and js files from google and include the path of the files in the href attribute of link tag and src attribute of the script tag respectively.

CONCLUSION:- I hope this article will help you to understand easily. If you have any doubt then please leave your comment below. If you want an alternate solution for the above example then visit here


2 thoughts on “How to calculate javascript difference between two dates in days”

  1. Wow that was strange. I just wrote an incredibly long comment but after I
    clicked submit my comment didn’t appear. Grrrr… well I’m not writing all
    that over again. Regardless, just wanted to say great blog!

    Reply
  2. Oh my goodness! Amazing article dude! Thanks,
    However I am having issues with your RSS. I don’t know the reason why I
    cannot join it. Is there anybody else getting the same RSS problems?
    Anybody who knows the answer will you kindly respond?

    Thanks!!

    Reply

Leave a Comment

KUPU178

KUPU178

RATU311

THOR311 TOTO4D

KUPU178

Live Casino Roullate

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

KUPU178

THOR311

THOR311

THOR311

RATU311

CERI188

DAFTAR KUPU178

LINK DAFTAR CERI188

CERI188

sabung ayam online

APK KUPU178

KUPU178

CERI188

CERI188

RATU311

KUPU178

THOR311 DOMINO QQ

judi bola online

Mix Parlay

CERI188

CERI188

CERI188

KUPU178

RATU311

live casino online

RATU311

Mix Parlay

Sabung Ayam Online

Slot PG Soft

CERI188

RATU311

RATU311

RATU311

RATU311

CERI188

THOR311

CERI188

THOR311

RATU311

THOR311

Game Thor311

THOR311

HOKI311

live casino

sv388

HOKI311

HOKI311

slot mahjong

Thor311 Alternatif

RATU311

HOKI311

HOKI311

HOKI311

HOKI311

CERI188

THOR311 RESMI

THOR311 AKSES

THOR311

THOR311 RTP

RATU311

RATU311

THOR311

parlay

HOKI311

RATU311

THOR311

Ceri188

Thor311 Game Online

Thor311 Aplikasi

LINK CERI188

RATU311 NONTON BOLA

RATU311

CERI188 Link RTP LIVE

THOR311

Thor311 Live Casino

CERI188

akses VIP THOR311

THOR311

link daftar THOR311

THOR311

situs thor311

THOR311 LOGIN

THOR311

THOR311

RATU311

RATU311

HOKI311 Link SABUNG AYAM RESMI

HOKI311 Link QRIS EWALLET

RATU311

LINK CERI188

parlay

HOKI311 Link RTP Gacor

THOR311

RATU311

THOR311

HOKI311 Link Apk Resmi

KUPU178

RATU311

RATU311

CERI188

CERI188 Situs Spaceman Terbaik 2026

KUPU178 Link Daftar

CERI188

CERI188

CERI188

CERI188

CERI188

HOKI311

THOR311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

CERI188 Live Casino

HOKI311 Lapak Arena Slot

RATU311 judi bola

CERI188 Live Casino

HOKI311 Link Spaceman Online

HOKI311 Link Daftar Resmi

CERI188 Toto SGP Online

KUPU178

KUPU178

KUPU178

KUPU178

HOKI311 TEMBAK IKAN ONLINE

RATU311

CERI188 PANDUAN MAXWIN SLOT ONLINE

RATU311

RATU311

KUPU178

HOKI311 Link Login Alternatif

HOKI311 Live Casino Online

HOKI311 Situs Slot Gacor

THOR311

THOR311

situs judi bola

Ceri188 Game Online

CERI188

link login CERI188

link login CERI188

CERI188

link login CERI188

link login CERI188

link daftar CERI188

THOR311

HOKI311

HOKI311

CERI188

CERI188

CERI188

parlay

THOR311

RATU311

RATU311

HOKI311

HOKI311

parlay

HOKI311

RATU311

THOR311

KUPU178

HOKI311

KUPU178

KUPU178

KUPU178

HOKI311

parlay

slot gacor

kupu178 link alternatif

kupu178 slot

kupu178 daftar

kupu178 login

link daftar kupu178

judi bola

HOKI311

RATU311

link alternatif RATU311

Link daftar RATU311

link login RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

KUPU178

THOR311

KUPU178

KUPU178

KUPU178

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

THOR311

RATU311

KUPU178

KUPU178

HOKI311

THOR311

KUPU178

THOR311

THOR311

slot thailand

judi bola

THOR311

THOR311

KUPU178

THOR311

situs bola

THOR311

slot gacor

slot gacor

yakuza303

slot gacor

unobet77

slot gacor

slot gacor

RATU311

parlay

parlay

slot gacor

mix parlay

judi bola

judi bola

mix parlay

parlay

judi bola

sv388

judi bola

judi bola

judi bola

parlay

THOR311