adding 1 year to a date field is showing wrong value.

vass2
Mega Expert

Hi All,

Here i am trying to add 1 year and fetch result into u_next_verification_date. when i execute in the message it shows correct , but in the field value showing wrong. 

Any help is highly appreciated.

Vass

find_real_file.png

When i update the record it is showing the correct  date values in the message below.

find_real_file.png

But when i open the record and check the field value it showing wrong. (Highlighted field).

Also i wish the date value should be the blue rounded one hours/minutes/seconds. 

find_real_file.png

 

1 ACCEPTED SOLUTION

Try Below Code,Its working fine

 

var gr = new GlideDateTime();
var gr1=gs.yearsAgo(-1);
var date= gr1.split(' ')[0];
var time=gr1.split(' ')[1];
var gd = new GlideDate();
gd.setValue(date);
date=gd.getByFormat('dd/MM/yyyy');
gr1=date+' '+time; //gr1 is your converted format
gs.addInfoMessage(gr1);

current.u_next_verification_date =gr1;

current.update();

View solution in original post

40 REPLIES 40

Hi Ravi,

Sorry for the dalay. Of course it is working fine , but there is a 1 hour difference. But I don't mind, because I need to find the difference in days .

But I am trying to find the difference of No.Days between current timestamp and verification date and place it in Days Since Last audit field.

 

find_real_file.png

 

I am trying with below code in display business rule. I am getting wrong no . days. Is this also something you can help me?

(function executeRule(current, previous /*null when async*/) {

 var gdt1 = new GlideDateTime(current.u_verification_date);
 var gdt4 = new GlideDateTime(gdt1).getDate().getByFormat("dd-MM-yyyy");

gs.addInfoMessage('gdt4 = ' + gdt4);
 
var gdt2 = new GlideDateTime(gs.nowDateTime());
var gdt3 = new GlideDateTime(gdt2).getDate().getByFormat("dd-MM-yyyy");
//var gdt2 = new GlideDateTime().getDate().getByFormat("dd-MM-yyyy");
gs.addInfoMessage('gdt3 = ' + gdt3);
var dur = GlideDateTime.subtract(gdt2, gdt1); //the difference between gdt1 and gdt2

gs.addInfoMessage('dur = ' + dur);
var dur1= new GlideDuration();

gs.addInfoMessage('dur1 = ' + dur1);
dur1.setValue(dur.getValue());

g_scratchpad.dateTime  = dur1.getDurationValue();
//current.<name of duration field>=dur1.getDurationValue();
gs.addInfoMessage('datetime = ' + g_scratchpad.dateTime);

 //g_scratchpad.dateTime=gs.nowDateTime();

})(current, previous);

Got the below values:

find_real_file.png

 

vass2
Mega Expert

Forgot to mention one more point here:

Once I get the duration value in scratchpad, I will assign the same in the field using a client script

 

Thanks

VASS

vass2
Mega Expert

Apologies, please ignore the above script , sorry for the confusion

 

(function executeRule(current, previous /*null when async*/) {

 var gdt1 = new GlideDateTime(current.u_verification_date);
gs.addInfoMessage('gdt1 = ' + gdt1);
 
var gdt2 = new GlideDateTime(gs.nowDateTime());
gs.addInfoMessage('gdt2 = ' + gdt2);

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

var dur1= new GlideDuration();
gs.addInfoMessage('dur1 = ' + dur1);

dur1.setValue(dur.getValue());
g_scratchpad.dateTime  = dur1.getDurationValue();
gs.addInfoMessage('datetime = ' + g_scratchpad.dateTime);

 g_scratchpad.dateTime=gs.nowDateTime();

})(current, previous);

 

Thanks
VASS

vass2
Mega Expert

Thanks Ravi and Archana for your continuous support and without you both I could not achieve this guys.

I got this working.

 

Regards,
VASS

Glad to hear that 🙂