This code will give you the date after 30 days and number of days between any two dates.Sometimes it is required in a program to get a particular date and the difference between two dates. This code is short but essentially effective.

Calculating The Date After 30 Days:

function  next_date()    {
var d1==$(“# current_ date”).val();
// The number of milliseconds in one day
var one_day=parseInt(1000*60*60*24);
//Here we need to split the inputed dates to convert them into standard format
var x=t1.split(“/”);
//date format(Fullyear,month,date)
var date1=new Date(x[2],x[1],(x[0]-1));
var thirty_days = new Date(date1.getTime() + 30*24*60*60*1000);
var date = thirty_days.getDate();
var month = thirty_days.getMonth();
var year = thirty_days.getFullYear();
var nw_date=date+’/'+month +’/'+year;
$(“#next_date”).val(nw_date);
}

Calculating  The Number  Of  Days Between  Any  Two  Dates:

function calculate(start_date,end_date)         {
var t1= start_date ;
var t2= end_date;
// The number of milliseconds in one day
var one_day=1000*60*60*24;
//Here we need to split the inputed dates to convert them into standard format
var x=t1.split(“/”);
var y=t2.split(“/”);
//date format(Fullyear,month,date)
var date1=new Date(x[2],(x[1]-1),x[0]);
var date2=new Date(y[2],(y[1]-1),y[0]);
var month1=x[1]-1;
var month2=y[1]-1;
//Calculate difference between the two dates, and convert to days
numberofDays=Math.ceil((date2.getTime()-date1.getTime())/(one_day));
// numberofDays gives the diffrence between the two dates.
$(‘#Input_type_text).val(numberofDays);
}

Ref: http://www.codeproject.com/KB/scripting/Javascript.aspx

Latest posts by paramita

  • Share/Bookmark