0

How to calculate days between two dates by using JavaScript?

  1. How to get only 'day' from the date.
  2. How to get only 'month' from the date.
  3. How to get only 'Year' from the date.
  4. How to calculate 'year' from the 'days'

This type of questions are have Answer is below refer it

R.Rajesh
  • 1
  • 4

3 Answers3

1

You can use Moment.Js to get the difference between two dates as below.

var start = moment("2015-13-08");
var end = moment("2015-13-08");
start.diff(end, "days")

for getting day, month, year you can use native javaScript.

var k = new Date();
k.getDate(); //gets the date
k.getMonth() + 1 ;  //gets the month index (0-11) starting from 0, so u will get 7 
                    //  for August. Added +1 to get the  month index on 1-12  scale
k.getFullYear() ; //gets the full year.

//Year from days
 var years =  days/365 ;  //days is the no of days.
sudhansu63
  • 6,025
  • 4
  • 39
  • 52
0

Choose Your Fromdate in given textbox

input name="datepicker" id="textboxid" type="text" readonly="readonly" placeholder="Select your fromdate" onchange="" 

1.your selecting date stored in one variable

var datepick = $("#textboxid").val();

//your choose date like 03/06/2015

2.you want to take day,month,year separately from the datepicker means use following format

 var fromdate = from.slice(0, 2);

// like 03

 var frommonth = from.slice(3, 5);

// like 06

 var fromyear = from.slice(6, 10);

//like 2015 3.After getting fromdate separately we move to today date calculation.

 var dateString = Date.now();

// We get date like dateString = 1439454966423

 var currentTime = new Date(parseInt(dateString));

//currentTime = Thu Aug 13 2015 14:06:06 GMT+0530 (India Standard Time) {}

4.current date are stored in one variable like below

 var to = currentTime.yyyymmdd();

//2015/08/13

5.you want to take day,month,year separately from the today date means use following format

 var todate = to.slice(8, 10);

// You get like 13

 var tomonth = to.slice(5, 7);

// You get like 08

 var toyear = to.slice(0, 4);

// You get like 2015

6.oneday calculation

 var oneDay = 24 * 60 * 60 * 1000;

7.you getting from date like(year,month,day) wise and todate(year,month,day) for diff calculation means use following format

 var firstDate = new Date(fromyear, frommonth, fromdate);

var secondDate = new Date(toyear, tomonth, todate);

8.below script used for days calculation between firstDate and secondDate.

 var diffDays = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime()) / (oneDay)));

9.we are calculating year from the diffdays.

 var getyear = Math.ceil(diffDays / 365);

10.To return formated datetime value you can use the following method.

Date.prototype.yyyymmdd = function () { var yyyy =this.getFullYear().toString(); var mm = (this.getMonth() +1).toString(); 

// getMonth() is zero-based var dd =this.getDate().toString(); return yyyy + "-" +(mm[1] ? mm : "0" +mm[0]) + "-" + (dd[1] ? dd : "0" + dd[0]); // padding };
Rahul Gopi
  • 414
  • 1
  • 16
  • 31
R.Rajesh
  • 1
  • 4
0

You might want to use moment.js and it's subtract method

Jon G Stødle
  • 3,844
  • 1
  • 16
  • 22