The GlideDateTime class is for working with date/time fields and date/times in general.
Constructing a new GlideDateTime object using the new
keyword with no arguments initializes it to the current date and time in GMT. You can also initialize while passing in an argument: either another GlideDateTime object or a date-formatting string (in the UTC timezone) in the format: yyyy-MM-dd HH:mm:ss.
There are multiple methods for adding or removing time from a GlideDateTime object once it's initialized. Here are a few of the more useful ones.
The add()
method accepts one of two types of arguments: either a GlideTime object (an object containing a specific duration or amount of time), or a number of milliseconds.
You can add a specific number of days (local time or UTC) using addDaysLocalTime()
or addDaysUTC()
respectively. Neither method returns a value.
The same is true for adding time week-by-week (addWeeksLocalTime()
and addWeeksUTC()
), month-by-month (addMonthsLocalTime()
and addMonthsUTC()
), and even year-by-year (addYearsLocalTime()
and addYearsUTC()
).
The following snippet demonstrates several ways to add time to a GlideDateTime object.
The following code declares a GlideDateTime object, initializes it with the current date and time, then adds 3 months and 2 days by first adding 24 hours in milliseconds, 1 day in days, and then 3 months. Finally, it sets a date/time field on the current
record to the value in the date/time object:
var gdt = new GlideDateTime(gs.nowDateTime()); //current date/time gdt.add(24*60*60*1000); //Add 24 hours gdt.addDaysLocalTime(1); //Add another day gdt.addMonthsLocalTime(3); //Add 3 months current.setValue('start_time', gdt.getValue());
One often has the need to figure out the day of the month or week, either right now, or for a date/time in a given field. For example, certain events should only occur on weekends, or on the first day of the month. These methods both have UTC and local time versions (getDayOfMonthUTC()
or getDayOfWeekLocalTime()
, for example).
These methods can be called on a GlideDateTime object that's already been initialized with a value. They take no arguments, but return a number value representing the day of the week (1-7) or the day of the month.
18.223.32.230