VOOZH about

URL: https://en.wikiversity.org/wiki/JavaScript/Date_calculator

⇱ JavaScript/Date calculator - Wikiversity


Jump to content
From Wikiversity

To calculate the difference between dates, the entered dates are first converted to Unix epoch time stamps, counting up from January 1, 1970. Then, the difference in seconds is converted back to days.

In order to return a positive value, the time stamps are then compared so the earlier time stamp is subtracted from the more recent one.

At the end, an object is generated which contains the number of days and a custom-generated text. It is stored into a variable so it can be alerted to the user.

The supported input date formats are "2026-04-05", "Apr 05, 2026" (with and without comma), and "05 Apr 2026". For example, calcDate("2021-01-01","2022-01-10") returns { total_days: 374, result: "1 year 9 days" }. The worded month can optionally be written out. Make sure not to miss quotation marks (both " and ' work), so the entered dates are interpreted as strings.

varcalcDate_data={};// initializing data object

functioncalcDate(date1,date2){

// memorize input to detect invalid input
calcDate_data.input1=date1;
calcDate_data.input2=date2;

// initiate date object
vardt_date1=newDate(date1);
vardt_date2=newDate(date2);
// get the time stamp
date1=dt_date1.getTime();
date2=dt_date2.getTime();

varcalc;
// check which time stamp is greater
if(date1>date2){
calc=newDate(date1-date2);
}else{
calc=newDate(date2-date1);
}

// retrieve the date, month and year
varcalc_format_tmp=calc.getDate()+'-'+(calc.getMonth()+1)+'-'+calc.getFullYear();
// convert to an array and store
varcalc_format=calc_format_tmp.split("-");
// subtract each member of our array from the default date
vardays_passed=parseInt(Math.abs(calc_format[0])-1);
varmonths_passed=parseInt(Math.abs(calc_format[1])-1);
varyears_passed=parseInt(Math.abs(calc_format[2]-1970));

// set up custom text
varyears_plural=["year","years"];
varmonths_plural=["month","months"];
vardays_plural=["day","days"];

// convert to days and sum together
vartotal_days=(years_passed*365)+(months_passed*30.417)+days_passed;

// display result with custom text
varresult="";// declare string
if(years_passed==1)result+=years_passed+' '+years_plural[0];
if(years_passed>1)result+=years_passed+' '+years_plural[1];
if(result!=""&&months_passed>0)result+=' ';// add space only if anything follows
if(months_passed==1)result+=months_passed+' '+months_plural[0];
if(months_passed>1)result+=months_passed+' '+months_plural[1];
if(result!=""&&days_passed>0)result+=' ';
if(days_passed==1)result+=days_passed+' '+days_plural[0];
if(days_passed>1)result+=days_passed+' '+days_plural[1];

// put last result into object so it can be alerted to the user
calcDate_data.last_result={
"total_days":Math.round(total_days),
"text":result
};

// message returned when an invalid date was input
if(!calcDate_data.last_result.text||calcDate_data.input1==null||calcDate_data.input2==null){
calcDate_data.last_result.text="At least one input date is invalid.";
}

// return the result
returncalcDate_data.last_result;

}

// example use
calcDate(
// prompt user to enter two dates
prompt("Date calculator \n Enter start date"),
prompt("Date calculator \n Enter end date")
);
alert(calcDate_data.last_result.text);// alert result text