- Mastering ServiceNow Scripting
- Andrew Kindred
- 320字
- 2021-06-24 19:08:39
GlideDateTime
GlideDateTime is, unsurprisingly, about scripting with dates and times, specifically the GlideDateTime object. The GlideDateTime object is mainly used to populate Date/Time fields and the methods around them to manipulate dates and times to add or remove time to a field.
First, let's look at defining a new GlideDateTime:
var glideDT = new GlideDateTime();
This will put the current date and time in GMT format in the glideDT field as part of a GlideDateTime object. This can be helpful if you want to compare a date in a field with the current date and time.
Now, with any date and time scripting, time zones are always an issue. One of the best ways to get around this is to use the display value to make sure dates and times are displayed to users in the correct format for the user who is viewing it. Let's look at how this is done by logging the display value:
var glideDT = new GlideDateTime();
gs.log(glideDT.getDisplayValue());
This is very helpful in scripting to ensure that all users see the correct times.
Sometimes there will also be the requirement to add or remove time from a Date/Time field using script. There are a few methods to add various amounts of time to or from a Date/Time field. We'll have a look at one of these; the others work in a very similar way:
var glideDT = new GlideDateTime();
glideDT.addDaysLocalTime(1);
In this example, we would be adding 1 day, so 24 hours, to the current day and time:
var glideDT = new GlideDateTime();
glideDT.addDaysLocalTime(-1);
By using a negative number, we actually subtract time from the object so the preceding example would actually give you the date and time of this time yesterday.
GlideDateTime is helpful when dealing with dates and times, which can often be a headache in coding. Remember, when using negative figures in addtime methods, this reduces the time instead of increasing it.
- 電氣自動(dòng)化專業(yè)英語(yǔ)(第3版)
- 大數(shù)據(jù)導(dǎo)論:思維、技術(shù)與應(yīng)用
- 計(jì)算機(jī)原理
- 并行數(shù)據(jù)挖掘及性能優(yōu)化:關(guān)聯(lián)規(guī)則與數(shù)據(jù)相關(guān)性分析
- Apache Hive Essentials
- 大數(shù)據(jù)技術(shù)入門(第2版)
- 網(wǎng)絡(luò)綜合布線技術(shù)
- 最簡(jiǎn)數(shù)據(jù)挖掘
- Windows程序設(shè)計(jì)與架構(gòu)
- 現(xiàn)代機(jī)械運(yùn)動(dòng)控制技術(shù)
- 大型數(shù)據(jù)庫(kù)管理系統(tǒng)技術(shù)、應(yīng)用與實(shí)例分析:SQL Server 2005
- 基于ARM 32位高速嵌入式微控制器
- Implementing Splunk 7(Third Edition)
- PostgreSQL 10 Administration Cookbook
- Statistics for Data Science