GlideDateTime is the server-side class for date and time values in ServiceNow scripts. Each object holds one moment. Create it from the current time, a string or another object, add or subtract intervals, compare, and read the result in UTC or in the user’s time zone. Methods named Local follow the user; methods named UTC follow UTC.
What a GlideDateTime object holds
A GlideDateTime object represents a single moment. Its internal form is a UTC string in the pattern yyyy-MM-dd HH:mm:ss, and that is what getValue() returns. The toString() method returns the same string as getValue().
That split between a stored UTC value and a per-user presentation explains the shape of the API. Most methods either work on the stored moment (UTC) or translate it through the time zone of the current user (Local). Once you know which side a method sits on, the rest of the names read naturally.
For a related server-side class, see GlideRecord explained.
Creating an object
There are three constructors.
new GlideDateTime()creates an object set to the current date and time.new GlideDateTime(value)creates an object from a date-time string that is expressed in UTC.new GlideDateTime(otherGdt)creates an object with the same time as an existing one.
The copy constructor matters more than it looks. When you need a modified version of a moment and want to keep the original, copy first and change the copy. The add methods change the object they are called on and return nothing, so there is no new object to capture.
With the string constructor, the system tries its internal format first, which is yyyy-MM-dd HH:mm:ss. If the string does not match, it works through a fixed list of other formats in order. The pattern yyyy-MM-dd’T’HH:mm:ss.SSSZ is not supported. An ambiguous string can be resolved to the wrong date because day and month are ambiguous, so when you control the input, pass the internal format.
var now = new GlideDateTime();
var fixed = new GlideDateTime("2025-03-04 09:30:00");
var copy = new GlideDateTime(fixed);
gs.info(fixed.getValue()); // the stored UTC string
gs.info(fixed.toString()); // same string as getValue()
Adding and subtracting time
The add methods change the object in place and return void.
| Method | What it adds |
|---|---|
addSeconds(n) |
seconds |
add(ms) |
milliseconds |
addDaysLocalTime(n) / addDaysUTC(n) |
days |
addWeeksLocalTime(n) / addWeeksUTC(n) |
weeks |
addMonthsLocalTime(n) / addMonthsUTC(n) |
months |
addYearsLocalTime(n) / addYearsUTC(n) |
years |
A negative number subtracts. So addDaysLocalTime(-1) moves the object back one day.
The Local variants work from local date and time values, and the UTC variants work from UTC values. Use the Local form when the rule follows the person who will see the result, and the UTC form for a rule tied to a UTC-based cutoff.
var due = new GlideDateTime("2025-03-04 09:30:00");
due.addSeconds(3600); // one hour later
due.addDaysLocalTime(2); // two days later by the user's calendar
due.addMonthsUTC(-1); // one month earlier by the UTC calendar
The numeric forms take milliseconds, so one minute is 60000. Seconds and milliseconds are easy to mix up: addSeconds(60) and add(60000) do the same thing.
Scheduled work is a common place for this arithmetic; see scheduled jobs explained.
Subtracting one moment from another
The static two-argument form, GlideDateTime.subtract(start, end), returns a GlideDuration describing the gap between two objects. It does not change either input.
var gdt1 = new GlideDateTime("2011-08-28 09:00:00");
var gdt2 = new GlideDateTime("2011-08-31 08:00:00");
var dur = GlideDateTime.subtract(gdt1, gdt2);
gs.info(dur.getDisplayValue()); // 2 Days 23 Hours
Keep this separate from the instance form subtract(ms), which moves the object itself backward by a number of milliseconds and returns nothing.
Comparing two moments
Five methods compare one object with another in scoped applications. The global GlideDateTime reference documents only compareTo() and equals() for this job.
after(gdt)is true when the object is later than the argument.before(gdt)is true when it is earlier.onOrAfter(gdt)is true when it is equal or later.onOrBefore(gdt)is true when it is equal or earlier.compareTo(o)returns 0 for equal, 1 when the object is later and -1 when it is earlier.
The equals() method accepts a GlideDateTime or a string and returns a boolean.
var start = new GlideDateTime("2025-03-04 08:00:00");
var end = new GlideDateTime("2025-03-04 17:00:00");
var check = new GlideDateTime();
if (check.onOrAfter(start) && check.before(end)) {
gs.info("Inside the window");
}
When a number is more convenient, getNumericValue() returns milliseconds since January 1, 1970, 00:00:00 GMT. The value for 2011-08-31 08:00:00 is 1314777600000. Two numeric values can be subtracted directly when you want a gap in milliseconds.
Reading values out
Two read methods cover everyday needs:
getValue()returns the UTC string in yyyy-MM-dd HH:mm:ss.getDisplayValue()returns a string in the current user’s format and time zone.
The display format can differ from one user to the next, so the string from getDisplayValue() is suited to showing a person, not to parsing in a business rule or script include. If code needs to branch on the value, use getValue(), the comparison methods or getNumericValue().
For the wider concept, see display value explained. Related pages cover business rules and script includes.
For example, for the stored value 2011-08-31 08:00:00 and a US/Pacific session, getDisplayValue() gives 2011-08-31 01:00:00.
Related readers worth knowing:
| Method | Returns |
|---|---|
getDisplayValueInternal() |
the user’s time zone, in yyyy-MM-dd HH:mm:ss |
getDisplayValueLang(style, language) |
a localized string; styles are full, long, medium and short, and an unrecognized style falls back to medium; language is a BCP-47 tag |
getDate() |
the date in yyyy-MM-dd in the system time zone |
getLocalDate() / getLocalTime() |
GlideDate and GlideTime in the user’s time zone |
getTZOffset() |
the time zone offset in milliseconds |
getDSTOffset() |
the daylight saving offset in milliseconds, or 0 |
isDST() |
true when daylight saving applies |
Pieces of a date
Calendar parts come in Local and UTC pairs: getYearLocalTime() and getYearUTC(), getMonthLocalTime() and getMonthUTC(), getDayOfMonthLocalTime() and getDayOfMonthUTC(), plus getDaysInMonthLocalTime() and getDaysInMonthUTC().
Two details trip people up. getDayOfWeekLocalTime() and getDayOfWeekUTC() return 1 to 7 with Monday as 1 and Sunday as 7. The week-of-year methods count weeks that begin on Sunday, and the highest value is 52 or 53.
Setting values
The setters mirror the readers.
setValue(value)takes a UTC string in the internal format. It also accepts a GlideDateTime or a number of milliseconds since the epoch. If the string does not match the internal format the system guesses, and an ambiguous string can be resolved to the wrong date because day and month are ambiguous.setValueUTC(dt, format)parses the string with the format you give, in UTC. It throws a runtime exception when the string does not match the format, andgetErrorMsg()returns the message.setDisplayValue(asDisplayed)takes a string in the current user’s format and time zone.setDisplayValue(value, format)parses with an explicit format in the user’s time zone. A mismatch throws a runtime exception.setDisplayValueInternal(value)takes yyyy-MM-dd HH:mm:ss in the user’s time zone.setGlideDateTime(g)copies the value from another object.- Single-part setters exist for the day of month, month and year, again in Local and UTC pairs. A day beyond the length of the month is set to the last day of that month.
Use the explicit-format versions whenever an incoming string is not already in the internal format. Build the pattern from tokens such as dd, MM, yyyy, HH, mm and ss, matching the incoming string exactly.
var gdt = new GlideDateTime();
try {
gdt.setDisplayValue("20-05-2025 12:00:00", "dd-MM-yyyy HH:mm:ss");
} catch (e) {
gs.error("Bad date: " + gdt.getErrorMsg());
}
When a parse fails, getErrorMsg() supplies the reason.
Local or UTC: choosing between them
A practical way to choose is to ask who the answer is for.
- Business logic that must come out the same for all users, such as a stored deadline or an integration timestamp, belongs in UTC. Use
getValue(), the UTC add methods and numeric comparison. - Logic that follows a person’s calendar, such as “tomorrow” or “start of the month” for the user, uses the Local methods.
- Text for a screen or an email uses
getDisplayValue(), converted at the last step.
A script that adds Local intervals and then logs getValue() can look wrong at first, because the log is in UTC while the arithmetic followed the user’s calendar. Both are views of one moment.
When a script misbehaves, the script debugger can help you step through it. For time-based conditions on service targets, see SLA start, pause and stop conditions.
Frequently asked questions
What does getValue() return in GlideDateTime?
It returns the stored value as a UTC string in the pattern yyyy-MM-dd HH:mm:ss. The toString() method returns the same string.
What is the difference between getValue() and getDisplayValue()?
getValue() gives the UTC string. getDisplayValue() gives a string in the current user’s format and time zone, and that format can differ between users, so it suits display rather than parsing.
How do I subtract days from a GlideDateTime?
Pass a negative number to addDaysLocalTime() or addDaysUTC(). The Local form follows the user’s calendar and the UTC form follows UTC.
How do I get the gap between two dates?
Call the static GlideDateTime.subtract(start, end). It returns a GlideDuration and leaves both inputs unchanged. getNumericValue() on each object also gives milliseconds that you can subtract.
What does GlideDateTime do when a date string does not match the internal format?
The constructor tries a fixed list of other formats in order, and setValue() guesses. An ambiguous string can be resolved to the wrong date because day and month are ambiguous, so setValueUTC() or setDisplayValue() with an explicit format is the safer route.