GlideDateTime

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.

Tip

An easy way to convert the value in a date/time field to a GlideDateTime object, is by using the getGlideObject() method of the GlideElement class. Example:

var gdtStart = current.start_time.getGlideObject()

Adding or removing time

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.

Example usage

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()); 

getDayOfMonth() and getDayOfWeek()

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.

Tip

Note that while getDayOfWeekUTC() and getDayOfWeekLocalTime() return a number, the returned value doesn't adhere to the standard Sunday-through-Saturday week. Monday is the first day of the week to a GlideDateTime object, and it returns the value 1. Sunday will return 7.

Example usage

The following snippet returns 7, since the day specified falls on a Sunday:

var gdt = new GlideDateTime("2017-12-03 12:00:00"); 
gs.info(gdt.getDayOfWeekLocalTime()); 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.32.230