logo

NJP

Fun with datetime default values

Import · Oct 07, 2011 · article

[imageWouldn't it be nice to be able to set smart date time default values that go beyond setting your start and end date by default to the date the record was created on?Well, you can do this by combining the rich datetime API provided by the GlideSystem object, accessible natively in the JavaScript layer and the Java class GlideDateTime to handle timezone conversions gracefully.Here is an example that is used as a meaningful default value for the start date of an entity that requires a bit of processing by several people before it can actually starts: let's make it start by default at the beginning of the hour two hours from now to avoid showing meaningless red alerts before the entity even gets a chance to get processed...in the 'Default' section of the Dictionary entry defining my datetime entity:javascript:// get the beginning of the hour two hours from now. i.e.: if now is 1:23pm, get 3pmvar startGMT = gs.hoursAgoStart(-2);// take care of the timezone conversionvar startLocal = new Packages.com.glide.glideobject.GlideDateTime(); startLocal.setValue(startGMT); // set the default valuestartLocal.getDisplayValue();

The image above is an artist rendition of the mental representation of having to deal with timezone conversations for most people.

View original source

https://www.servicenow.com/community/in-other-news/fun-with-datetime-default-values/ba-p/2282942