This topic is locked
[SOLVED]

 Calculate Date Difference Using Javascript

2/7/2013 9:04:27 AM
PHPRunner General questions
S
Suety29 author

Hi,
I have 2 date fields (start date and end date) which are simple edit boxes with datepickers. I am trying to calculate the difference in days between these 2 fields after the user selects the dates using the date picker. Can anyone assist?

Admin 2/7/2013

There is a Javascript function that accepts to Date objects and calculates difference in days. Make sure you convert whatever date values you have to Javascript Date objects first.

function days_between(date1, date2) {
// The number of milliseconds in one day

var ONE_DAY = 1000 * 60 * 60 * 24
// Convert both dates to milliseconds

var date1_ms = date1.getTime()

var date2_ms = date2.getTime()
// Calculate the difference in milliseconds

var difference_ms = Math.abs(date1_ms - date2_ms)



// Convert back to days and return

return Math.round(difference_ms/ONE_DAY)
}


Credit goes to http://www.mcfedries.com/javascript/daysbetween.asp
More info on Javascript Date object:

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date

S
Suety29 author 2/7/2013



There is a Javascript function that accepts to Date objects and calculates difference in days. Make sure you convert whatever date values you have to Javascript Date objects first.

function days_between(date1, date2) {
// The number of milliseconds in one day

var ONE_DAY = 1000 * 60 * 60 * 24
// Convert both dates to milliseconds

var date1_ms = date1.getTime()

var date2_ms = date2.getTime()
// Calculate the difference in milliseconds

var difference_ms = Math.abs(date1_ms - date2_ms)
// Convert back to days and return

return Math.round(difference_ms/ONE_DAY)
}


Credit goes to http://www.mcfedries.com/javascript/daysbetween.asp
More info on Javascript Date object:

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date


i tried what you suggested but no values shows up in my days field on the page. Which event would i need to use since the date is being selected from a datepicker?

Admin 2/8/2013

The function that I have posted simply calculates the date difference. It doesn't mean to update any fields on any page.
If you want to perform a certain action on field change check this example for inspiration:

http://xlinesoft.com/phprunner/docs/how_to_calculate_values_on_the_fly.htm
You will need to implement 'change' event handler in similar fashion and update another field value.

S
Suety29 author 2/21/2013



The function that I have posted simply calculates the date difference. It doesn't mean to update any fields on any page.
If you want to perform a certain action on field change check this example for inspiration:

http://xlinesoft.com/phprunner/docs/how_to_calculate_values_on_the_fly.htm
You will need to implement 'change' event handler in similar fashion and update another field value.


Hi below is the code i tried using but nothing is happening in the Days field:

var ctrlArrDate = Runner.getControl(pageid,'Arr_Date');

var ctrlDepDate = Runner.getControl(pageid,'Dep_Date');

var ctrlDays = Runner.getControl(pageid,'Days');
function days_between(date1, date2) {
// The number of milliseconds in one day

var ONE_DAY = 1000 * 60 * 60 * 24
// Convert both dates to milliseconds

//var date1_ms = date1.getTime()

//var date2_ms = date2.getTime()



// Calculate the difference in milliseconds

var difference_ms = Math.abs(date1_ms - date2_ms)



// Convert back to days and return

return Math.round(difference_ms/ONE_DAY)
};
// Store the arrival date and time

var Arr_date = new Date(getTime(ctrlArrDate))
// Store the departure date

var Dep_date = new Date(getTime(ctrlDepDate))
//End_date.setYear(End_date.getFullYear() + 1)

//End_date.setMonth(0)

//End_date.setDate(1)
// Call the days_between function

var days_left = days_between(Arr_date, Dep_date)
function setDays(){
ctrlDays.setValue(days_left);
};
ctrlDepDate.on('change',setDays);
Admin 2/21/2013

I guess there is an error somewhere in your Javascript code. Here is the article that explains how to troubleshoot Javascript code:

http://xlinesoft.com/blog/2012/05/22/how-to-troubleshoot-javascript-errors/
If you need assistance from support team post your application to Demo Account and open a ticket at http://support.xlinesoft.com sending your Demo Account URL. 'Demo Account' button can be found on the last screen in the program.

C
cgphp 2/21/2013

I have made some adjustments. Please, check the following code:

var ctrlArrDate = Runner.getControl(pageid,'Arr_Date');

var ctrlDepDate = Runner.getControl(pageid,'Dep_Date');

var ctrlDays = Runner.getControl(pageid,'Days');
function days_between(date1, date2) {
// The number of milliseconds in one day

var ONE_DAY = 1000 * 60 * 60 * 24;



// Calculate the difference in milliseconds

var difference_ms = Math.abs(date1 - date2);



// Convert back to days and return

return Math.round(difference_ms/ONE_DAY)
}
// Store the arrival date and time

var Arr_date = new Date(getTime(ctrlArrDate.getValue()));
// Store the departure date

var Dep_date = new Date(getTime(ctrlDepDate.getValue()));
// Call the days_between function

var days_left = days_between(Arr_date, Dep_date);
function setDays(){

ctrlDays.setValue(days_left);

}
ctrlDepDate.on('change',setDays);
S
Suety29 author 2/21/2013



I have made some adjustments. Please, check the following code:

var ctrlArrDate = Runner.getControl(pageid,'Arr_Date');

var ctrlDepDate = Runner.getControl(pageid,'Dep_Date');

var ctrlDays = Runner.getControl(pageid,'Days');
function days_between(date1, date2) {
// The number of milliseconds in one day

var ONE_DAY = 1000 * 60 * 60 * 24;
// Calculate the difference in milliseconds

var difference_ms = Math.abs(date1 - date2);
// Convert back to days and return

return Math.round(difference_ms/ONE_DAY)
}
// Store the arrival date and time

var Arr_date = new Date(getTime(ctrlArrDate.getValue()));
// Store the departure date

var Dep_date = new Date(getTime(ctrlDepDate.getValue()));
// Call the days_between function

var days_left = days_between(Arr_date, Dep_date);
function setDays(){

ctrlDays.setValue(days_left);

}
ctrlDepDate.on('change',setDays);



Hi,
thanks for the responses. Cristian Gilè i tried your code but still, nothing shows up in the days field. I started a support ticket but my level of support doesn't cover coding support <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=69943&image=1&table=forumreplies' class='bbc_emoticon' alt=':(' /> . Really need to get this working like yesterday. Any help offered is greatly appreciated.