GlideDateTime - Scoped

The scoped GlideDateTime class provides methods for performing operations on GlideDateTime objects, such as instantiating GlideDateTime objects or working with glide_date_time fields.

Use the GlideDateTime methods to perform date-time operations, such as instantiating a GlideDateTime object, performing date-time calculations, formatting a date-time, or converting between date-time formats.

Scoped GlideDateTime - add(GlideTime gd)

Adds a GlideTime object to the current GlideDateTime object.

Table 1. Parameters
Name Type Description
gd GlideTime The GlideTime object to add.
Table 2. Returns
Type Description
void
var gdt = new GlideDateTime("2011-08-31 08:00:00");
var gtime1 = new GlideTime();
gtime1.setValue("00:00:20");
gdt.add(gtime1);
var gtime2 = gdt.getTime();
gs.info(gtime2.getByFormat('hh:mm:ss'));

Output:

Scoped GlideDateTime - add(Number milliseconds)

Adds the specified number of milliseconds to the current GlideDateTime object.

Table 3. Parameters
Name Type Description
milliseconds Number The number of milliseconds to add.
Table 4. Returns
Type Description
void
var gdt = new GlideDateTime("2011-08-31 08:00:00");
gs.info(gdt.getNumericValue());
gdt.add(10);
gs.info(gdt.getNumericValue());

Output: 1314777600000 1314777600010

Scoped GlideDateTime - addDaysLocalTime(Number days)

Adds a specified number of days to the current GlideDateTime object. A negative parameter subtracts days. The method determines the local date and time equivalent to the value stored by the GlideDateTime object, then adds or subtracts days using the local date and time values.

Table 5. Parameters
Name Type Description
days Number The number of days to add. Use a negative value to subtract.
Table 6. Returns
Type Description
void
var gdt = new GlideDateTime("2011-08-31 08:00:00");
gdt.addDaysLocalTime(-1);
gs.info(gdt.getLocalDate());

Output: 2011-08-30

Scoped GlideDateTime - addDaysUTC(Number days)

Adds a specified number of days to the current GlideDateTime object. A negative parameter subtracts days. The method determines the UTC date and time equivalent to the value stored by the GlideDateTime object, then adds or subtracts days using the UTC date and time values.

Table 7. Parameters
Name Type Description
days Number The number of days to add. Use a negative number to subtract.
Table 8. Returns
Type Description
void
var gdt = new GlideDateTime("2011-08-31 08:00:00");
gdt.addDaysUTC(-1);
gs.info(gdt.getDate());

Output: 2011-08-30

Scoped GlideDateTime - addMonthsLocalTime(Number months)

Adds a specified number of months to the current GlideDateTime object. A negative parameter subtracts months. The method determines the local date and time equivalent to the value stored by the GlideDateTime object, then adds or subtracts months using the local date and time values.

Table 9. Parameters
Name Type Description
months Number The number of months to add. use a negative value to subtract.
Table 10. Returns
Type Description
void
var gdt = new GlideDateTime("2011-08-31 08:00:00");
gdt.addMonthsLocalTime(2);
gs.info(gdt.getDate());

Output: 2011-10-31

Scoped GlideDateTime - addMonthsUTC(Number months)

Adds a specified number of months to the current GlideDateTime object. A negative parameter subtracts months. The method determines the UTC date and time equivalent to the value stored by the GlideDateTime object, then adds or subtracts months using the UTC date and time values.

Table 11. Parameters
Name Type Description
months Number The number of months to add. Use a negative value to subtract.
Table 12. Returns
Type Description
void
var gdt = new GlideDateTime("2011-08-31 08:00:00");
gdt.addMonthsUTC(2);
gs.info(gdt.getDate());

Output:

Scoped GlideDateTime - addSeconds(Number seconds)

Adds the specified number of seconds to the current GlideDateTime object.

Table 13. Parameters
Name Type Description
seconds Number The number of seconds to add.
Table 14. Returns
Type Description
void
var gdt = new GlideDateTime("2011-12-07 08:00:00");
gdt.addSeconds(1000);
gs.info(gdt.getValue());

Output: 2011-12-07 08:16:40

Scoped GlideDateTime - addWeeksLocalTime(Number weeks)

Adds a specified number of weeks to the current GlideDateTime object. A negative parameter subtracts weeks. The method determines the local date and time equivalent to the value stored by the GlideDateTime object, then adds or subtracts weeks using the local date and time values.

Table 15. Parameters
Name Type Description
weeks Number The number of weeks to add. Use a negative value to subtract.
Table 16. Returns
Type Description
void
var gdt = new GlideDateTime("2011-08-31 08:00:00");
gdt.addWeeksLocalTime(-1);
gs.info(gdt.getDate());

Output: 2011-08-24

Scoped GlideDateTime - addWeeksUTC(Number weeks)

Adds a specified number of weeks to the current GlideDateTime object. A negative parameter subtracts weeks. The method determines the UTC date and time equivalent to the value stored by the GlideDateTime object, then adds or subtracts weeks using the UTC date and time values.

Table 17. Parameters
Name Type Description
weeks Number The number of weeks to add. Use a negative value to subtract.
Table 18. Returns
Type Description
void
var gdt = new GlideDateTime("2011-08-31 08:00:00");
gdt.addWeeksUTC(-1);
gs.info(gdt.getDate());

Output: 2011-08-24

Scoped GlideDateTime - addYearsLocalTime(Number years)

Adds a specified number of years to the current GlideDateTime object. A negative parameter subtracts years. The method determines the local date and time equivalent to the value stored by the GlideDateTime object, then adds or subtracts years using the local date and time values.

Table 19. Parameters
Name Type Description
years Number The number of years to add. Use a negative value to subtract.
Table 20. Returns
Type Description
void
var gdt = new GlideDateTime("2010-08-31 08:00:00");
gdt.addYearsLocalTime(1);
gs.info(gdt.getDate());

Output: 2011-08-31

Scoped GlideDateTime - addYearsUTC(Number years)

Adds a specified number of years to the current GlideDateTime object. A negative parameter subtracts years. The date and time value stored by GlideDateTime object is interpreted as being in the UTC time zone.

Table 21. Parameters
Name Type Description
years Number The number of years to add. Use a negative value to subtract.
Table 22. Returns
Type Description
void
var gdt = new GlideDateTime("2010-08-31 08:00:00");
gdt.addYearsUTC(1);
gs.info(gdt.getDate());

Output: 2011-08-31

Scoped GlideDateTime - after(GlideDateTime gdt)

Determines if the GlideDateTime object occurs after the specified GlideDateTime.

Table 23. Parameters
Name Type Description
gdt GlideDateTime The time to check against.
Table 24. Returns
Type Description
Boolean Returns true if the GlideDateTime object's time is after the time specified by the parameter.
var gdt1 = new GlideDateTime("2016-05-09 10:11:12");
var gdt2 = new GlideDateTime("2017-06-12 15:11:12");
gs.info(gdt1.after(gdt2)); 

Output: False

Scoped GlideDateTime - before(GlideDateTime gdt)

Determines if the GlideDateTime object occurs before the specified GlideDateTime.

Table 25. Parameters
Name Type Description
gdt GlideDateTime The time to check against.
Table 26. Returns
Type Description
Boolean Returns true if the GlideDateTime object's time is before the time specified by the parameter.
var gdt1 = new GlideDateTime("2016-05-09 10:11:12");
var gdt2 = new GlideDateTime("2017-06-12 15:11:12");
gs.info(gdt1.before(gdt2));  

Output: True

Scoped GlideDateTime - compareTo(Object o)

Compares two date and time objects to determine whether they are equivalent or one occurs before or after the other.

Table 27. Parameters
Name Type Description
o Object Date and time object in GlideDateTime format
Table 28. Returns
Type Description
Number
  • 0 = Dates are equal
  • 1 = Initial date is before the compare date
  • -1 = Initial date is after the compare date
var initDate = new GlideDateTime("2011-08-01 12:00:00");
var compDate1 = new GlideDateTime("2011-08-01 12:00:00");
var compDate2 = new GlideDateTime("2011-07-31 12:00:00");
var compDate3 = new GlideDateTime("2011-08-04 16:00:00");
 
gs.info(initDate.compareTo(compDate1)); // Equals
gs.info(initDate.compareTo(compDate2)); // Earlier than
gs.info(initDate.compareTo(compDate3)); // Later than

Scoped GlideDateTime - equals(Object dateTime)

Compares a datetime with an existing value for equality.

Table 29. Parameters
Name Type Description
dateTime GlideDateTime object or String The datetime to compare.
Table 30. Returns
Type Description
Boolean Returns true if they are equal; otherwise, false.
var gdt = new GlideDateTime("2011-08-31 00:00:00");
gs.info(gdt.equals("2011-09-30 00:12:01"));

Output: false

Scoped GlideDateTime - getDate()

Gets the date stored by the GlideDateTime object, expressed in the standard format, yyyy-MM-dd, and the system time zone, UTC by default.

Table 31. Parameters
Name Type Description
None
Table 32. Returns
Type Description
GlideDate The date in the system time zone.
var gdt = new GlideDateTime("2011-08-31 08:00:00");
gs.info(gdt.getDate());

Output:

Scoped GlideDateTime - getDayOfMonthUTC()

Gets the day of the month stored by the GlideDateTime object, expressed in the UTC time zone.

Table 33. Parameters
Name Type Description
None
Table 34. Returns
Type Description
Number The day of the month in the UTC time zone, from 1 to 31.
var gdt = new GlideDateTime("2011-12-02 12:00:00");
gs.info(gdt.getDayOfMonthUTC());

Output: 02

Scoped GlideDateTime - getDayOfMonthLocalTime()

Gets the day of the month stored by the GlideDateTime object, expressed in the current user's time zone.

Table 35. Parameters
Name Type Description
None
Table 36. Returns
Type Description
Number The day of the month in the user's time zone, from 1 to 31.
var gdt = new GlideDateTime("2011-12-02 12:00:00");
gs.info(gdt.getDayOfMonthLocalTime());

Output:

Scoped GlideDateTime - getDayOfWeekUTC()

Gets the day of the week stored by the GlideDateTime object, expressed in the UTC time zone.

Table 37. Parameters
Name Type Description
None
Table 38. Returns
Type Description
Number The day of week value from 1 to 7. Monday equals 1, Sunday equals 7.
var gdt = new GlideDateTime("2011-12-01 12:00:00");//Thursday
gs.info(gdt.getDayOfWeekLocalTime());

Output:

Scoped GlideDateTime - getDayOfWeekLocalTime()

Gets the day of the week stored by the GlideDateTime object, expressed in the user's time zone.

Table 39. Parameters
Name Type Description
None
Table 40. Returns
Type Description
Number The day of week value, in the user's time zone, from 1 to 7. Monday equals 1, Sunday equals 7.
var gdt = new GlideDateTime("2011-12-01 12:00:00");//Thursday
gs.info(gdt.getDayOfWeekLocalTime());

Output:

Scoped GlideDateTime - getDaysInMonthLocalTime()

Gets the number of days in the month stored by the GlideDateTime object, expressed in the current user's time zone.

Table 41. Parameters
Name Type Description
None
Table 42. Returns
Type Description
Number The number of days in the current month in the user's time zone.
var gdt = new GlideDateTime("2011-12-02 12:00:00"); //December
gs.info(gdt.getDaysInMonthLocalTime());

Output:

Scoped GlideDateTime - getDaysInMonthUTC()

Gets the number of days in the month stored by the GlideDateTime object, expressed in the UTC time zone.

Table 43. Parameters
Name Type Description
None
Table 44. Returns
Type Description
Number The number of days in the month stored by the GlideDateTime object, expressed in the UTC time zone.
var gdt = new GlideDateTime("2011-11-02 12:00:00"); //November
gs.info(gdt.getDaysInMonthUTC());

Output:

Scoped GlideDateTime - getDisplayValue()

Gets the date and time value in the current user's display format and time zone.

Table 45. Parameters
Name Type Description
None
Table 46. Returns
Type
String - The date and time in the user's format and time zone. Keep in mind when designing business rules or script includes that this method may return values in different formats for different users.
var gdt = new GlideDateTime("2011-08-31 08:00:00");
gs.info(gdt.getDisplayValue()); //uses current user session time zone (US/Pacific)

Output:

Scoped GlideDateTime - getDisplayValueInternal()

Gets the display value in the internal format (yyyy-MM-dd HH:mm:ss).

Table 47. Parameters
Name Type Description
None
Table 48. Returns
Type Description
String The date and time values for the GlideDateTime object in the current user's time zone and the internal date and time format of yyyy-MM-dd HH:mm:ss.
var gdt = new GlideDateTime("2011-08-31 08:00:00"); 
gs.info(gdt.getDisplayValueInternal()); //uses current user session time zone (US/Pacific)

Output:

Scoped GlideDateTime - getDSTOffset()

Gets the amount of time that daylight saving time is offset.

Table 49. Parameters
Name Type Description
None
Table 50. Returns
Type Description
Number Amount of time, in milliseconds, that daylight saving is offset. Returns 0 if there is no offset or if the time is not during daylight saving time.
var gdt = new GlideDateTime("2014-08-31 08:00:00"); 
gs.info(gdt.getDSTOffset()); //uses current user session time zone (US/Pacific)

Output:

Scoped GlideDateTime - getErrorMsg()

Gets the current error message.

Table 51. Parameters
Name Type Description
None
Table 52. Returns
Type Description
String The error message.
var gdt = new GlideDateTime(); 
gdt.setDisplayValue("2011-aa-01 00:00:00"); 
gs.info(gdt.getErrorMsg());

Output: Could not parse DateTime: 2011-aa-01 00:00:00

Scoped GlideDateTime - getInternalFormattedLocalTime()

Returns the object's time in the local time zone and in the internal format.

Table 53. Parameters
Name Type Description
None
Table 54. Returns
Type Description
String The object's time in the local time zone and the internal format.

Scoped GlideDateTime - getLocalDate()

Gets the date stored by the GlideDateTime object, expressed in the standard format, yyyy-MM-dd, and the current user's time zone.

Table 55. Parameters
Name Type Description
None
Table 56. Returns
Type
GlideDate The date in the user's time zone.
var gdt = new GlideDateTime("2011-08-31 08:00:00");
gs.info(gdt.getLocalDate());

Output:

Scoped GlideDateTime - getLocalTime()

Returns a GlideTime object that represents the time portion of the GlideDateTime object in the user's time zone.

Table 57. Parameters
Name Type Description
None
Table 58. Returns
Type Description
GlideTime The time in the user's time zone.
var gdt = new GlideDateTime("2014-08-31 08:00:00");
gt = gdt.getLocalTime();
gs.info("local time is " + gt.getByFormat('hh:mm:ss'));

Output:

Scoped GlideDateTime - getMonthLocalTime()

Gets the month stored by the GlideDateTime object, expressed in the current user's time zone.

Table 59. Parameters
Name Type Description
None
Table 60. Returns
Type Description
Number The numerical value of the month.
var gdt = new GlideDateTime("2011-11-02 12:00:00"); //November
gs.info(gdt.getMonthLocalTime());

Output:

Scoped GlideDateTime - getMonthUTC()

Gets the month stored by the GlideDateTime object, expressed in the UTC time zone.

Table 61. Parameters
Name Type Description
None
Table 62. Returns
Type Description
Number The numerical value of the month.
var gdt = new GlideDateTime("2011-11-02 12:00:00"); //November
gs.info(gdt.getMonthUTC());

Output:

Scoped GlideDateTime - getNumericValue()

Gets the number of milliseconds since January 1, 1970, 00:00:00 GMT.

Table 63. Parameters
Name Type Description
None
Table 64. Returns
Type Description
Number The number of milliseconds since January 1, 1970, 00:00:00 GMT.
var gdt = new GlideDateTime("2011-08-31 08:00:00");
gs.info(gdt.getNumericValue());

Output:

Scoped GlideDateTime - getTime()

Returns a GlideTime object that represents the time portion of the GlideDateTime object.

Table 65. Parameters
Name Type Description
None
Table 66. Returns
Type Description
GlideTime The Unix duration stamp in system format based on GMT time.
var gdt = new GlideDateTime("2014-08-31 08:00:00");
gt = gdt.getTime();
gs.info(gt.getByFormat('hh:mm:ss'));

Output:

Scoped GlideDateTime - getTZOffset()

Gets the time zone offset in milliseconds.

Table 67. Parameters
Name Type Description
None
Table 68. Returns
Type Description
Number The number of milliseconds of time zone offset.
var gdt = new GlideDateTime();
gdt.getLocalTime(); // PST local time
gs.info(gdt.getTZOffset());

Output: -25200000

Scoped GlideDateTime - getUserFormattedLocalTime()

Returns the object's time in the local time zone and in the user's format.

Table 69. Parameters
Name Type Description
None
Table 70. Returns
Type Description
String The object's time in the local time zone and in the user's format.

Scoped GlideDateTime - getValue()

Gets the date and time value stored by the GlideDateTime object in the internal format, yyyy-MM-dd HH:mm:ss, and the system time zone, UTC by default.

Table 71. Parameters
Name Type Description
None
Table 72. Returns
Type
String - The date and time value in the internal format and system time zone.
var gdt = new GlideDateTime("2014-08-31 08:00:00");
gs.info(gdt.getValue());

Output:

Scoped GlideDateTime - getWeekOfYearUTC()

Gets the number of the week stored by the GlideDateTime object, expressed in the UTC time zone. All weeks begin on Sunday. The first week of the year is the week that contains at least one day of the new year. The week beginning Sunday 2015-12-27 is considered the first week of 2016 as that week contains January 1 and 2.

Table 73. Parameters
Name Type Description
None
Table 74. Returns
Type
Number - The number of the current week in UTC time. The highest week number in a year is either 52 or 53.
var gdt = new GlideDateTime("2011-12-01 12:00:00");//49th week, 1st week in december 
gs.info(gdt.getWeekOfYearUTC());

Output:

Scoped GlideDateTime - getWeekOfYearLocalTime()

Gets the number of the week stored by the GlideDateTime object, expressed in the current user's time zone. All weeks begin on Sunday. The first week of the year is the week that contains at least one day of the new year. The week beginning Sunday 2015-12-27 is considered the first week of 2016 as that week contains January 1 and 2.

Table 75. Parameters
Name Type Description
None
Table 76. Returns
Type
Number - The number of the current week in local time. The highest week number in a year is either 52 or 53.
var gdt = new GlideDateTime("2011-12-01 12:00:00");//49th week, 1st week in december 
gs.info(gdt.getWeekOfYearLocalTime());

Output:

Scoped GlideDateTime - getYearLocalTime()

Gets the year stored by the GlideDateTime object, expressed in the current user's time zone.

Table 77. Parameters
Name Type Description
none
Table 78. Returns
Type Description
Number Four-digit year value in the user's time zone.
var gdt = new GlideDateTime("2011-11-02 12:00:00");
gs.info(gdt.getYearLocalTime());

Output:

Scoped GlideDateTime - getYearUTC()

Gets the year stored by the GlideDateTime object, expressed in the UTC time zone.

Table 79. Parameters
Name Type Description
None
Table 80. Returns
Type Description
Number 4-digit year value in the UTC time zone.
var gdt = new GlideDateTime("2011-11-02 12:00:00"); 
gs.info(gdt.getYearUTC());

Output:

Scoped GlideDateTime - GlideDateTime()

Instantiates a new GlideDateTime object with the current date and time in Greenwich Mean Time (GMT).

Table 81. Parameters
Name Type Description
None
var gdt = new GlideDateTime();

Scoped GlideDateTime - GlideDateTime(GlideDateTime g)

Instantiates a new GlideDateTime object set to the time of the GlideDateTime object passed in the parameter.

Table 82. Parameters
Name Type Description
g GlideDateTime The GlideDateTime object to use for setting the time of the new object.
var start = new GlideDateTime("2011-01-01 12:00:00");
var end = new GlideDateTime(start);
gs.info(end);

Output:

Scoped GlideDateTime - GlideDateTime(String value)

Instantiates a new GlideDateTime object from a date and time value in the UTC time zone specified with the format yyyy-MM-dd HH:mm:ss.

Table 83. Parameters
Name Type Description
value String A UTC date and time using the internal format yyyy-MM-dd HH:mm:ss.
var gdt = new GlideDateTime("2011-01-01 12:00:00");

Output:

Scoped GlideDateTime - hasDate()

Determines if an object's date is set.

Table 84. Parameters
Name Type Description
None
Table 85. Returns
Type Description
Boolean True if the object date is set; otherwise, returns false.
var gdt = new GlideDateTime("2011-08-31 08:00:00");
gs.info(gdt.hasDate());

Output:

Scoped GlideDateTime - isDST()

Determines if an object's time uses a daylight saving offset.

Table 86. Parameters
Name Type Description
None
Table 87. Returns
Type Description
Boolean True if the time is daylight saving; otherwise, returns false.
var gdt = new GlideDateTime("2014-08-31 00:00:00");
gs.info(gdt.isDST()); //true

Output:

Scoped GlideDateTime - isValid()

Determines if a value is a valid date and time.

Table 88. Parameters
Name Type Description
None
Table 89. Returns
Type Description
Boolean True if value is valid; otherwise, returns false.
var gdt = new GlideDateTime(); 
gdt.setDisplayValue("2011-aa-01 00:00:00"); 
gs.info(gdt.isValid());

Output: false

Scoped GlideDateTime - onOrAfter(GlideDateTime gdt)

Determines if the GlideDateTime object occurs on or after the specified GlideDateTime.

Table 90. Parameters
Name Type Description
gdt GlideDateTime The time to check against.
Table 91. Returns
Type Description
Boolean Returns true if the GlideDateTime object's time is on or after the time specified by the parameter.
var gdt1 = new GlideDateTime("2016-05-09 10:11:12");
var gdt2 = new GlideDateTime("2017-06-12 15:11:12");
gs.info(gdt1.onOrAfter(gdt2));   

Output: False

Scoped GlideDateTime - onOrBefore(GlideDateTime gdt)

Determines if the GlideDateTime object occurs on or before the specified GlideDateTime.

Table 92. Parameters
Name Type Description
gdt GlideDateTime The time to check against.
Table 93. Returns
Type Description
Boolean Returns true if the GlideDateTime object's time is on or before the time specified by the parameter.
var gdt1 = new GlideDateTime("2016-05-09 10:11:12");
var gdt2 = new GlideDateTime("2017-06-12 15:11:12");
gs.info(gdt1.onOrBefore(gdt2));   

Output: True

Scoped GlideDateTime - setDayOfMonthLocalTime(Number day)

Sets the day of the month to a specified value in the current user's time zone.

Table 94. Parameters
Name Type Description
day Number The day of month to change to, from 1 to 31. If this value is greater than the maximum number of days in the month, the value is set to the last day of the month.
Table 95. Returns
Type Description
void
var gdt = new GlideDateTime();
gdt.setDayOfMonthLocalTime(9);
gs.info(gdt.getDayOfMonthLocalTime());

Output:

Scoped GlideDateTime - setDayOfMonthUTC(Number day)

Sets the day of the month to a specified value in the UTC time zone.

Table 96. Parameters
Name Type Description
day Number The day of month to change to, from 1 to 31. If this value is greater than the maximum number of days in the month, the value is set to the last day of the month.
Table 97. Returns
Type Description
void
var gdt = new GlideDateTime();
gdt.setDayOfMonthUTC(9);
gs.info(gdt.getDayOfMonthUTC());

Output:

Scoped GlideDateTime - setDisplayValue(String asDisplayed)

Sets a date and time value using the current user's display format and time zone.

Table 98. Parameters
Name Type Description
asDisplayed String The date and time in the current user's display format and time zone. The parameter must be formatted using the current user's preferred display format, such as MM-dd-yyyy HH:mm:ss. To assign the current date and time to a variable in a workflow script, use variable.setDisplayValue(gs.nowDateTime);.
Table 99. Returns
Type Description
void
var gdt = new GlideDateTime("2014-02-02 12:00:00");
gdt.setDisplayValue("2014-01-01 12:00:00");//uses current user session time zone (US/Pacific) 
gs.info(gdt.getValue());

Output:

Scoped GlideDateTime - setDisplayValue(String value, String format)

Sets a date and time value using the current user's time zone and the specified date and time format. This method throws a runtime exception if the date and time format used in the value parameter does not match the format parameter. You can retrieve the error message by calling getErrorMsg() on the GlideDateTime object after the exception is caught.

Table 100. Parameters
Name Type Description
value String The date and time in the current user's time zone.
format String The date and time format to use to parse the value parameter.
Table 101. Returns
Type Description
void
var gdt = new GlideDateTime("2011-02-02 12:00:00");
gdt.setDisplayValue("20-5-2011 12:00:00", "dd-MM-yyyy HH:mm:ss"); //uses current user session time zone (US/Pacific)
gs.info(gdt.getValue());

Output:

Scoped GlideDateTime - setDisplayValueInternal(String value)

Sets a date and time value using the internal format (yyyy-MM-dd HH:mm:ss) and the current user's time zone.

Table 102. Parameters
Name Type Description
value String The date and time in internal format.
Table 103. Returns
Type Description
void
var gdt = new GlideDateTime("2014-02-02 12:00:00");
gdt.setDisplayValueInternal("2014-01-01 12:00:00"); //uses current user session time zone (US/Pacific)
gs.info(gdt.getValue());

Output:

Scoped GlideDateTime - setGlideDateTime(GlideDateTime g)

Sets the date and time of the current object using an existing GlideDateTime object. This method is equivalent to instantiating a new object with a GlideDateTime parameter.

Table 104. Parameters
Name Type Description
g GlideDateTime The object to use for setting the datetime value.
Table 105. Returns
Type Description
void
var dt1 = new GlideDateTime("2011-01-01 12:00:00");
var dt2 = new GlideDateTime("2011-02-02 08:00:00");
dt1.setGlideDateTime(dt2);
gs.info(dt1.getValue());

Output:

Scoped GlideDateTime - setMonthLocalTime(Number month)

Sets the month stored by the GlideDateTime object to the specified value using the current user's time zone.

Table 106. Parameters
Name Type Description
month Number The month to change to.
Table 107. Returns
Type Description
void
var gdt = new GlideDateTime();
gdt.setMonthLocalTime(1);
gs.info(gdt.getMonthLocalTime());

Output: 1

Scoped GlideDateTime - setMonthUTC(Number month)

Sets the month stored by the GlideDateTime object to the specified value using the UTC time zone.

Table 108. Parameters
Name Type Description
month Number The month to change to.
Table 109. Returns
Type Description
void
var gdt = new GlideDateTime();
gdt.setMonthUTC(1);
gs.info(gdt.getMonthUTC());

Output: 1

Scoped GlideDateTime - setValue(String o)

Sets the date and time of the GlideDateTime object.

Table 110. Parameters
Name Type Description
o String The date and time to use. This parameter may be one of several types:
  • A string in the UTC time zone and the internal format of yyyy-MM-dd HH:mm:ss. Sets the value of the object to the specified date and time. Using the method this way is equivalent to instantiating a new GlideDateTime object using the GlideDateTime(String value) constructor. If the date and time format used does not match the internal format, the method attempts to set the date and time using other available formats. Resolving the date and time this way can lead to inaccurate data due to ambiguity in the day and month values. When using a non-standard date and time format, use etValueUTC(String dt, String format) instead.
  • A GlideDateTime object. Sets the value of the object to the date and time stored by the GlideDateTime passed in the parameter. Using the method this way is equivalent to instantiating a new GlideDateTime object using the GlideDateTime(GlideDateTime g) constructor.
  • A JavaScript Number. Sets the value of the object using the Number value as milliseconds past January 1, 1970 00:00:00 GMT.
Table 111. Returns
Type Description
void
var gdt = new GlideDateTime("2011-01-01 12:00:00");
gdt.setValue("2011-02-02 08:00:00");  // value set =  2011-02-02 08:00:00
gs.info(gdt.getValue());

Output:

Scoped GlideDateTime - setValueUTC(String dt, String format)

Sets a date and time value using the UTC time zone and the specified date and time format. This method throws a runtime exception if the date and time format used in the dt parameter does not match the format parameter. You can retrieve the error message by calling getErrorMsg() on the GlideDateTime object after the exception is caught.

Table 112. Parameters
Name Type Description
dt String The date and time to use.
format String The date and time format to use.
Table 113. Returns
Type Description
void
var gdt = new GlideDateTime("2011-01-01 12:00:00");
gdt.setValueUTC("15-02-2011 08:00:00", "dd-MM-yyyy HH:mm:ss");
gs.info(gdt.getValue());

Output:

Scoped GlideDateTime - setYearLocalTime(Number year)

Sets the year stored by the GlideDateTime object to the specified value using the current user's time zone.

Table 114. Parameters
Name Type Description
year Number The year to change to.
Table 115. Returns
Type Description
void
var gdt = new GlideDateTime();
gdt.setYearLocalTime(2013);
gs.info(gdt.getYearLocalTime());

Output: 2013

Scoped GlideDateTime - setYearUTC(Number year)

Sets the year stored by the GlideDateTime object to the specified value using the UTC time zone.

Table 116. Parameters
Name Type Description
year Number The year to change to.
Table 117. Returns
Type Description
void
var gdt = new GlideDateTime();
gdt.setYearUTC(2013);
gs.info(gdt.getYearUTC());

Output: 2013

Scoped GlideDateTime - subtract(GlideTime time)

Subtracts a specified amount of time from the current GlideDateTime object.

Table 118. Parameters
Name Type Description
time GlideTime The time value to subtract.
Table 119. Returns
Type Description
void
var gdt = new GlideDateTime("2011-08-31 08:00:00");
var gtime1 = new GlideTime();
gtime1.setValue("00:00:20");
gdt.subtract(gtime1);
var gtime2 = gdt.getTime();
gs.info(gtime2.getByFormat('hh:mm:ss'));

Output: 07:59:40

ScopedGlideDateTime - subtract(Number milliseconds)

Subtracts the specified number of milliseconds from the GlideDateTime object.

Table 120. Parameters
Name Type Description
milliseconds Number The number of milliseconds to subtract.
Table 121. Returns
Type Description
void
var gdt = new GlideDateTime("2011-12-07 08:00:00");
gdt.subtract(1000);
gs.info(gdt.getValue());

Output: 2011-12-07 07:59:59

Scoped GlideDateTime - subtract(GlideDateTime start, GlideDateTime end)

Gets the duration difference between two GlideDateTime values.

Table 122. Parameters
Name Type Description
Start GlideDateTime The start value.
End GlideDateTime The end value.
Table 123. Returns
Type Description
GlideDuration The duration between the two values.
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); //the difference between gdt1 and gdt2
gs.info(dur.getDisplayValue());

Output: 2 Days 23 Hours

Scoped GlideDateTime - toString()

Gets the date and time value stored by the GlideDateTime object in the internal format, yyyy-MM-dd HH:mm:ss, and the system time zone, UTC by default. This method is equivalent to getValue().

Table 124. Parameters
Name Type Description
None
Table 125. Returns
Type
String The date and time stored by the GlideDateTime object in the system time zone and format.
var gdt = new GlideDateTime("2011-08-31 08:00:00");
gs.info(gdt.toString());

Output: