This topic is locked

Guide 51 – Operations with Dates in JavaScript

2/8/2023 2:30:41 PM
PHPRunner Tips and Tricks
fhumanes author

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:

img alt

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');
var ctrl_Days = Runner.getControl(pageid, 'days');
var ctrl_End = Runner.getControl(pageid, 'Date_end');

ctrl_Days.setValue(0); // Defauklt Value Only ADD

// ctrl_End.makeReadonly();

ctrl_Ini.on('change', function() {
let value_init = ctrl_Ini.getValue();
// console.log(value_init);
let days = parseInt(ctrl_Days.getValue()); // Important, parse to INT
value_end = value_init.addDays(days);
// console.log(value_end);
ctrl_End.setValue(value_end);
});
ctrl_Days.on('change', function() {
let value_init = ctrl_Ini.getValue();
// console.log(value_init);
let days = parseInt(ctrl_Days.getValue()); // Important, parse to INT
value_end = value_init.addDays(days);
// console.log(value_end);
ctrl_End.setValue(value_end);
});

ctrl_End.on('change', function() {
let value_init = ctrl_Ini.getValue();
let value_end = ctrl_End.getValue();
let value_days = datediff( value_init, value_end);
ctrl_Days.setValue(value_days);
});

function datediff(first, second) {
return Math.round((second - first) / (1000 * 60 * 60 * 24));
}
Date.prototype.addDays = function(days) {
let date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
}

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.
calculating the expiry date with javascript.
Congratulations on your very successful work.
I follow your website and other works closely.

fhumanes author 2/9/2023

Thank you, @abidcastaneda and @ruzgarajans

Greetings,
fernando

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)
{

var diff =(dt2.getTime() - dt1.getTime()) / 1000;
diff /= 60;
return Math.abs(Math.round(diff));

}

So it would be a case of taking fhumanes code and inserting the above...

Dalkeith 2/10/2023

img alt

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,
fernando