Date Help !!

SandeepKSingh
Kilo Sage

How can you configure a form field so that, no matter what a user tries—whether picking yesterday, tomorrow, or even a year back—the only value the system stubbornly accepts is today’s date, straight from the system clock?

5 ACCEPTED SOLUTIONS

Dr Atul - LNG
Tera Patron

Hi @SandeepKSingh 

 

You can use a UI Policy to ensure that the entered date is today or a future date.
Set the condition accordingly and apply it on the same field.

If you want to add validation on the backend as well, you can include an error message using a UI Policy script.
Try it out — this approach should work for your use case.

View solution in original post

Ravi Gaurav
Giga Sage

Hi @SandeepKSingh 

As suggested by @Dr Atul - LNG  we can go with UI POLICY....
or you can write the below code:-

var gdt = new GlideDateTime();

var todayStr = gdt.getLocalDate().toString();

if (current.date_of_entry.toString() !== todayStr)

{

current.date_of_entry = todayStr;

gs.addErrorMessage('The date of entry must be the current date.'); current.setAbortAction(true);

}

View solution in original post

glideFather
Tera Patron

Hi @SandeepKSingh,

 

Wouldn't it be easier from a UX perspective to just make the field read-only and default it to today’s date?

 

If users can only select today and nothing else, giving them the option to edit the field feels a bit pointless.

Why let them change it if there's no real choice?

 

Create a variable and default value can be "javascript: new GlideDateTime();" and just make it read only..

 

GlideFather_0-1757340757490.png

View solution in original post

@SandeepKSingh 

 

Below is for reference,

Bhuvan_0-1757341036769.pngBhuvan_1-1757341056622.png

Sample dictionary entry,

Bhuvan_2-1757341337173.png

If this helped to answer your query, please mark it helpful & accept the solution.

 

Thanks,

Bhuvan

View solution in original post

12 REPLIES 12

@SandeepKSingh 

 

Yes, you can use GlideDateTime();

 

Bhuvan_0-1757345126915.png

Bhuvan_1-1757345140564.png

https://www.servicenow.com/docs/bundle/zurich-api-reference/page/app-store/dev_portal/API_reference/...

 

If this helped to answer your query, please mark it helpful & accept the solution.

 

Thanks,

Bhuvan

Ankur Bawiskar
Tera Patron

@SandeepKSingh 

Since you always want it to have now date/time as per logged in user's timezone, you can configure the default value and make that field readonly

No UI policy or client script required.

javascript: new GlideDateTime().getDisplayValue();

If my response helped please mark it correct and close the thread so that it benefits future readers.