glide date time difference

Deepika54
Tera Contributor

hello experts,

i have a glide date time value which is in "2022-11-17 02:45:00" format. I need to subtract 5 hours 30 mins from it. Can anyone tell me how to do it. I am new to Scripting.

Thanks in advance.

1 ACCEPTED SOLUTION

Sagar Pagar
Tera Patron

Hi @Deepika54,

 

Try this scripts -

var dateTime = new GlideDateTime('2022-11-17 02:45:00');

var dateTimeObj = new GlideDateTime(dateTime);
dateTimeObj.addSeconds(-19800); // 5.5 hrs = 19800 seconds

gs.info("Given date-time: " + dateTime + " - after date-time: " + dateTimeObj);

 

Thanks,
Saagr Pagar

The world works with ServiceNow

View solution in original post

4 REPLIES 4

Anand Shukla
Mega Guru

Hi Deepika

Below script will be helful for you.

 

var dateField = new GlideDateTime('2021-05-04');

var nowTime = new GlideDateTime();

var dur = new GlideDuration();

dur = GlideDateTime.subtract(dateField, nowTime);

var days = dur.getDayPart();

gs.info(days);

 

Mark it correct or helpful.

 

Thanks

i need to subtract only the time part. not the date

@Deepika54 

Try below code.

 

var gdt = new GlideDateTime("2011-08-31 08:00:00");
var gtime1 = new GlideTime();
gtime1.setValue("05:30:00");
gdt.subtract(gtime1);
gs.info(gdt );

 

newhand_0-1677564508802.png

 

Please mark my answer as correct and helpful based on Impact.

Sagar Pagar
Tera Patron

Hi @Deepika54,

 

Try this scripts -

var dateTime = new GlideDateTime('2022-11-17 02:45:00');

var dateTimeObj = new GlideDateTime(dateTime);
dateTimeObj.addSeconds(-19800); // 5.5 hrs = 19800 seconds

gs.info("Given date-time: " + dateTime + " - after date-time: " + dateTimeObj);

 

Thanks,
Saagr Pagar

The world works with ServiceNow