Guide 51 – Operations with Dates in JavaScript |
2/8/2023 2:30:41 PM |
PHPRunner Tips and Tricks | |
![]() One of the people who request my help asked me how to calculate a date starting from another date to which the days of a field of a form are added. These operations needed to be done in the browser, so it should be developed in JavaScript. Reading the PHPRunner manual and some other examples on the internet I have managed to do the calculation and, in addition, I have extended it to obtain the days between two dates. Aim Do calculations using date fields in JavaScript. Technical Solution The example I've made looks like this:
As we can see we have 3 fields. An initial date, a number of days that we have to add and an end date, which is calculated with the first 2 fields. Also, if the final date is modified, the application calculates the days difference between the initial and final dates. The code of the event “ javascript Onload event ” in ADD, is: var ctrl_Ini = Runner.getControl(pageid, 'date_ini'); In EDIT, it's the same code except line 5 is commented out or removed. I hope it helps you and for any need for an explanation, let me know through my email fernandohumanes@gmail.com |
|
![]() |
abidcastaneda 2/8/2023 |
I follow your website and I love it, thanks for all the tutorials, this is the first time I see you here. Saludos! |
![]() |
ruzgarajans 2/8/2023 |
Choose a date and calculate the due date instantly. |
![]() |
fhumanes author 2/9/2023 |
Thank you, @abidcastaneda and @ruzgarajans Greetings, |
S
|
Simtrain 2/10/2023 |
Is there a way to calculate time? i.e. start time, end time hours worked? |
![]() |
Dalkeith 2/10/2023 |
Here's a Javascript function that calculates time in minutes between two datetimes function diff_minutes(dt2, dt1) So it would be a case of taking fhumanes code and inserting the above... |
![]() |
Dalkeith 2/10/2023 |
|
![]() |
fhumanes author 2/10/2023 |
Hi @Simtrain, In the example the days are calculated, between 2 dates. If in the function that this calculation makes, in the dividend, we remove 24, 60 or another 60, you can calculate hours, minutes or seconds, as a difference from the 2 dates. Greetings, |