Change the date format to perform the calculation

122932
Tera Expert
Hello how are you?

I need to calculate the difference between two dates, but it's calculating wrong, 
because of the date formatting Example: I need to convert "2021-12-06 08:00:00" to "06-12-2021 08:00:00" to see if it calculates right, this is the script I'm using:

Business Rule:

var gdt1 = new GlideDateTime(current.getDisplayValue('u_data_inicio_lb2'));
var gdt2 = new GlideDateTime(current.getDisplayValue('u_data_termino_lb2'));

var dur = GlideDateTime.subtract(gdt1, gdt2); //the difference between gdt1 and gdt2

current.setValue("u_duracao_lb2",dur);

 

Gracias!
1 ACCEPTED SOLUTION

Hi, 

 

You will need to change the format of date and test it. I tried this script and it returned me 32 days. I have hardcoded the dates. Please change it.

var gdt = new GlideDateTime("01/11/2021 08:00:00");
var myDate = gdt.getDate(); 
var gt = gdt.getTime();

var fd = myDate.getByFormat('dd-MM-yyy') + gt.getByFormat(' HH:mm:ss')
gs.info(fd);  //

var gdts = new GlideDateTime("03/12/2021 08:00:00");
var myDates = gdts.getDate();
var gts = gdts.getTime();

var fds = myDates.getByFormat('dd-MM-yyy') + gts.getByFormat(' HH:mm:ss')
gs.info(fds);

var time1 = new GlideDateTime(fd);
var time2 = new GlideDateTime(fds);
var dur = GlideDateTime.subtract(time1, time2);


gs.info(dur.getDisplayValue());

 

Output showed as -

*** Script: 11-01-2021 08:00:00
*** Script: 12-03-2021 08:00:00
*** Script: 32 Days

View solution in original post

11 REPLIES 11

sonali panda1
Kilo Sage

Hi,

 

Are you in scoped application? With the subtract it will return as days and hours.

Try something like this to get in seconds 

var time1 = new GlideDateTime('u_data_inicio_lb2');
var time2 = new GlideDateTime('u_data_termino_lb2');
var dur = GlideDateTime.subtract(time1, time2);

var duration = dur.getNumericValue();
var durationSeconds = (duration/1000);

gs.info(dur.getDisplayValue());
gs.info(durationSeconds );

 

Thanks

Sonali

Thanks for the help, but it didn't work.

The fields: var gdt1 = new GlideDateTime(current.getDisplayValue('u_data_inicio_lb2')); < ---- Type Date/Time var gdt2 = new GlideDateTime(current.getDisplayValue('u_data_termino_lb2'));
< ---- Type Date/Time
and the return one: 
dur = GlideDateTime.subtract(gdt1, gdt2)
current.setValue("u_duracao_lb2",dur); <----Type Duration

when I do the calculation in the format 2021-12-06 08:00:00 It works, But when I use
06-12-2021 08:00:00 it calculates but calculates wrong.


Can you help me?

Your system time is stored in which format dd-mm-yyyy or mm-dd-yyyy?

Also in the below script did you pass the value for 'u_data_inicio_lb2 ? I just wrote the field name but you would need to add the way you had new GlideDateTime(current.getDisplayValue('u_data_inicio_lb2'));

 

var time1 = new GlideDateTime('u_data_inicio_lb2');
var time2 = new GlideDateTime('u_data_termino_lb2');

I tried this in PDI

 

var time1 = new GlideDateTime('23-12-2021 08:00:00');
var time2 = new GlideDateTime('25-12-2021 09:00:00');
var dur = GlideDateTime.subtract(time1, time2);

gs.info(dur.getDisplayValue());

 

It returned as 2 days 1 hour which is correct.