How to set default value of date/time field 24 hours from current time

redth
Giga Expert

Hi,

How to set default value of date/time field 24 hours from current time and restrict the user to not select date/time for more than 72 hours from current time

5 REPLIES 5

Yousaf
Giga Sage

Hi Redth.

Please check this untested code 

var date = new GlideDateTime();


var hours = 60*60*13;


date.addSeconds(hours);


gs.info(date.getDisplayValue());


Mark Correct or helpful if it helps.

***Mark Correct or Helpful if it helps.***

Hi, I have used the similar code. But it's not working as per my requirement. The above code adds one day from today, but I need to take account of time too. If the current date/time is 8/11/2022 10:23:00, it should reflect 8/12/2022 10:23:00. Instead, the above script just shows 8/12/2022 00:00:00

than try adding 

addDaysLocalTime(1) OR addDaysUTC(1)

above script will be 
var date = new GlideDateTime();
date.addDaysLocalTime(1)
gs.info(date.getDisplayValue());

 

https://developer.servicenow.com/dev.do#!/reference/api/tokyo/server_legacy/c_GlideDateTimeAPI#r_GDT...


***Mark Correct or Helpful if it helps.***

Pavankumar_1
Mega Patron

Hi,

Please try below script.

 

var gdt = GlideDateTime();

var daysToAdd= 1;
var day = GlideDateTime(gdt);
day.addDaysLocalTime(daysToAdd);
var d = day.getDate();
var t = day.getTime();

var newFormattedDate = d.getByFormat("dd-MM-yyyy");
var newFormattedTime = t.getByFormat('hh:mm:ss');

gs.info(newFormattedDate + ' ' +newFormattedTime);

 

Thanks,

Pavankumar

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar