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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Vaas,

That's because your system date time format is dd-mm-yyyy HH:MM:SS but the calculation you are doing is using system format i.e. YYYY-MM-DD HH:MM:SS.

you need to convert it into that format and then do the calculation

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

vass2
Mega Expert

Thanks Ankur.

May i know how to change that? I have tried different formats not working

 

Regards

VASS

Hi,

this is not the date time  format problem.

Actually i am also facing same problem so i first  i split date and time .

i added splited date in glidedatetime object .

then added 1 year to the date .

then combined date having 1 year added with time which i splited from the intial date .

so it is giving correct result.

 

so please use below code as i executed it and it's working .

var date ="2016-10-12 07:11:23";//here assign your actual date time and then split in two string 
var split_date =date.split(' ');

var gd =new GlideDateTime();
gd.setDisplayValue(split_date[0]);
gd.addYearsLocalTime(1);
var demp= gd.getDate();
var date_time = demp +" "+split_date[1];
gs.print( " date time after adding 1 year in actual date is "+date_time +" "+"only date is"+demp);

Mark this answer as Correct and Helpful .

Regards,

Manjusha Bangale

 

Archana Reddy2
Tera Guru

Hi,

Remove getDisplayValue from the code and give a try.