---
sourceDocument: Xanadu API Reference
sourceDocumentLink: https://servicenow-prod.fluidtopics.net/r/xanadu/api-reference

 Release :

    - xanadu

ft:locale :

    - en-US

ft:publication_title :

    - Xanadu API Reference

ft:clusterId :

    - crapiref

bundleId :

    - crapiref

workflow :

    - Creator


---

# GlideSchedule - Scoped

# GlideSchedule - Scoped {#ariaid-title1}

The scoped GlideSchedule API provides methods for performing operations on GlideSchedule objects, such as adding new schedule segments to a schedule, determining if a date/time is within the schedule, or
setting the schedule timezone.

## GlideSchedule - GlideSchedule() {#ariaid-title2}

Instantiates an empty GlideSchedule object.
{#r_ScopedGlideScheduleGlideSchedule__table_ulv_hhr_2r__entry__3}

| Name | Type | Description |
|-|-|-|
| None |   |   |
[Table 1. Parameters]

{#r_ScopedGlideScheduleGlideSchedule__table_ulv_hhr_2r}

## GlideSchedule - GlideSchedule(String sysID, String timeZone) {#ariaid-title3}

Instantiates a GlideSchedule object and loads the schedule information. If a time zone is not specified, the current session time zone is used.
{#r_SGSCH-GlideSchedule_S_S__table_emk_4cg_1r__entry__3}

| Name | Type | Description |
|-|-|-|
| sysID | String | The system ID for the schedule. |
| timeZone | String | Optional. The time zone to use. Default: The current session time zone. Time zones can be provided in the following formats. * `Country/City`. For example, `America/Los_Angeles`. * `Country/Time zone`. For example, `US/Pacific`. * `Time zone abbreviation`. For example, `PST`. {#r_SGSCH-GlideSchedule_S_S__ul_up2_zln_lwb}For a complete list of valid timezones, see the Time zone field in the User \[sys_user\] table. For more information about time zones, see [Time zones](https://www.servicenow.com/docs/access?context=r_TimeZones&version=xanadu&pubname=xanadu-platform-administration&ft:locale=en-US). |
[Table 2. Parameters]

{#r_SGSCH-GlideSchedule_S_S__table_emk_4cg_1r}  

    var schedule = new GlideSchedule('090eecae0a0a0b260077e1dfa71da828', 'US/Pacific');

## GlideSchedule - add(GlideDateTime startDate, GlideDuration offSet) {#ariaid-title4}

Adds a new schedule segment to the current schedule.
{#r_SGSCH-Add_GDT_GD__table_kws_rdg_1r__entry__3}

| Name | Type | Description |
|-|-|-|
| startDate | GlideDateTime | The starting date of the new schedule segment. |
| offSet | GlideDuration | The time offset of the new schedule segment. |
[Table 3. Parameters]

{#r_SGSCH-Add_GDT_GD__table_kws_rdg_1r} {#r_SGSCH-Add_GDT_GD__table_mws_rdg_1r__entry__2}

| Type | Description |
|-|-|
| GlideDateTime | The schedule updated with the new schedule segment. |
[Table 4. Returns]

{#r_SGSCH-Add_GDT_GD__table_mws_rdg_1r}  

    var startDate = new GlideDateTime('2014-01-02');
    var days = 2;
    var dur = new GlideDuration(60 * 60 * 24 * 1000 * days);
    var schedule = new GlideSchedule();
    var end = schedule.add(startDate, dur);
    gs.info(end);

Output:

    2014-01-04 00:00:00

## GlideSchedule - duration(GlideDateTime startDate, GlideDateTime endDate) {#ariaid-title5}

Determines the elapsed time in the schedule between two date time values using the
timezone of the schedule or, if that is not specified, the timezone of the session.
{#r_SGSCH-Duration_GDT_GDT__table_dlk_j3g_1r__entry__3}

| Name | Type | Description |
|-|-|-|
| startDate | GlideDateTime | The starting datetime. |
| endDate | GlideDateTime | The ending datetime. |
[Table 5. Parameters]

{#r_SGSCH-Duration_GDT_GDT__table_dlk_j3g_1r} {#r_SGSCH-Duration_GDT_GDT__table_flk_j3g_1r__entry__2}

| Type | Description |
|-|-|
| GlideDuration | The difference between the starting and ending datetime. |
[Table 6. Returns]

{#r_SGSCH-Duration_GDT_GDT__table_flk_j3g_1r}  

    var startDate = new GlideDateTime('2014-10-16 02:00:00');
    var endDate = new GlideDateTime('2014-10-18 04:00:00');
    var schedule = new GlideSchedule();
     
    schedule.load('090eecae0a0a0b260077e1dfa71da828'); // loads "8-5 weekdays excluding holidays" schedule
    var duration = schedule.duration(startDate, endDate);
    gs.info(duration.getDurationValue()); // gets the elapsed time in schedule

## GlideSchedule - getName() {#ariaid-title6}

Retrieves the schedule name.
{#r_ScopedGlideScheduleGetName__table_onn_hhr_2r__entry__3}

| Name | Type | Description |
|-|-|-|
| None |   |   |
[Table 7. Parameters]

{#r_ScopedGlideScheduleGetName__table_onn_hhr_2r} {#r_ScopedGlideScheduleGetName__table_tcd_rwg_1r__entry__2}

| Type | Description |
|-|-|
| String | The name of the current schedule. |
[Table 8. Returns]

{#r_ScopedGlideScheduleGetName__table_tcd_rwg_1r}  

    sys_id ='04e664654a36232701a2247dcd8fc4cf'; // sys_id for "Application" schedule record
    var sched = new GlideSchedule(sys_id);
    gs.info(sched.getName());

## GlideSchedule - isInSchedule(GlideDateTime time) {#ariaid-title7}

Determines if the specified date and time is within the current schedule.
{#r_SGSCH-IsInSchedule_GDT__table_lvd_jkg_1r__entry__3}

| Name | Type | Description |
|-|-|-|
| time | GlideDateTime | Date and time value to check. |
[Table 9. Parameters]

{#r_SGSCH-IsInSchedule_GDT__table_lvd_jkg_1r} {#r_SGSCH-IsInSchedule_GDT__table_nvd_jkg_1r__entry__2}

| Type | Description |
|-|-|
| Boolean | Flag that indicates whether the specified date and time is within the schedule. Valid values: * true: Date and time is within the schedule. * false: Date and time are outside of the schedule. {#r_SGSCH-IsInSchedule_GDT__ul_ujh_rxv_p4b} |
[Table 10. Returns]

{#r_SGSCH-IsInSchedule_GDT__table_nvd_jkg_1r}  

    var glide = new GlideRecord('cmn_schedule');
    glide.addQuery('type', 'blackout');
    glide.query();
    if (glide.next()) {
       var sched = new GlideSchedule(glide.sys_id);
       var date = new GlideDateTime();
       date.setDisplayValue("2007-09-18 12:00:00");
       if (sched.isInSchedule(date)) 
          gs.info("Is in the schedule");
       else
          gs.info("Is NOT in the schedule");
    }

## GlideSchedule - isValid() {#ariaid-title8}

Determines if the current schedule is valid. A schedule is valid if it has at least one
schedule span.
{#r_ScopedGlideScheduleIsValid__table_th1_plg_1r__entry__2}

| Type | Description |
|-|-|
| Boolean | True if the schedule is valid. |
[Table 11. Returns]

{#r_ScopedGlideScheduleIsValid__table_th1_plg_1r}  

    var glide = new GlideRecord('cmn_schedule');
    glide.addQuery('type', 'blackout');
    glide.query();
    if (glide.next()) {
       var sched = new GlideSchedule(glide.sys_id);
       var date = new GlideDateTime();
       date.setDisplayValue("2007-09-18 12:00:00");
       if (sched.isValid()) 
          gs.info("Is valid");
     
       else
          gs.info("Is not valid");
    }

## GlideSchedule - load(String sysID, String timeZone, String excludeSpanID) {#ariaid-title9}

Loads a schedule with the schedule information.
{#r_SGSCH-Load_S_S_S__table_ztn_lxg_1r__entry__3}

| Name | Type | Description |
|-|-|-|
| sysID | String | The system ID of the schedule. |
| timeZone | String | (Optional) The timezone. If a timezone is not specified, or is nil, the current session timezone is used for the schedule. |
| excludeSpanID | String | Any span to exclude. |
[Table 12. Parameters]

{#r_SGSCH-Load_S_S_S__table_ztn_lxg_1r} {#r_SGSCH-Load_S_S_S__table_tcd_rwg_1r__entry__2}

| Type | Description |
|-|-|
| void |   |
[Table 13. Returns]

{#r_SGSCH-Load_S_S_S__table_tcd_rwg_1r}  

    var x = new GlideSchedule();
    x.load('08fcd0830a0a0b2600079f56b1adb9ae');

## GlideSchedule - setTimeZone(String timeZone) {#ariaid-title10}

Sets the timezone for the current schedule.
{#r_SGSCH-SetTimeZone_S__table_sdh_hzg_1r__entry__3}

| Name | Type | Description |
|-|-|-|
| timeZone | String | The time zone to use. Time zones can be provided in the following formats. * `Country/City`. For example, `America/Los_Angeles`. * `Country/Time zone`. For example, `US/Pacific`. * `Time zone abbreviation`. For example, `PST`. {#r_SGSCH-SetTimeZone_S__ul_up2_zln_lwb}For a complete list of valid timezones, see the Time zone field in the User \[sys_user\] table. For more information about time zones, see [Time zones](https://www.servicenow.com/docs/access?context=r_TimeZones&version=xanadu&pubname=xanadu-platform-administration&ft:locale=en-US). |
[Table 14. Parameters]

{#r_SGSCH-SetTimeZone_S__table_sdh_hzg_1r} {#r_SGSCH-SetTimeZone_S__table_tcd_rwg_1r__entry__2}

| Type | Description |
|-|-|
| void |   |
[Table 15. Returns]

{#r_SGSCH-SetTimeZone_S__table_tcd_rwg_1r}  
This example sets the timezone for the schedule to US/Pacific.

    var schedule = new GlideSchedule();
    schedule.setTimeZone('US/Pacific');

## GlideSchedule - whenNext(GlideDateTime time, String timeZone) {#ariaid-title11}

Determines how much time (in milliseconds) until start time of the next schedule
item.
This function is intended to be called when the GlideSchedule object (cmn_schedule table)
is not currently in the schedule window. The whenNext() call returns duration (in ms) until
the GlideSchedule object is within the schedule. This function does not return a meaningful
value if called when the GlideSchedule object is within the schedule.
{#r_SGSCH-WhenNext_GDT_S__table_i1b_myg_1r__entry__3}

| Name | Type | Description |
|-|-|-|
| time | GlideDateTime | Time to be evaluated |
| timeZone | String | Timezone |
[Table 16. Parameters]

{#r_SGSCH-WhenNext_GDT_S__table_i1b_myg_1r} {#r_SGSCH-WhenNext_GDT_S__table_k1b_myg_1r__entry__2}

| Type | Description |
|-|-|
| Number | Number of milliseconds until the start time of the next schedule item. Returns -1 if never. |
[Table 17. Returns]

{#r_SGSCH-WhenNext_GDT_S__table_k1b_myg_1r}  

    var startDate = new GlideDateTime('2014-10-25 08:00:00');
    var glideSchedule = new GlideSchedule('08fcd0830a0a0b2600079f56b1adb9ae', 'UTC');
    gs.info(glideSchedule.whenNext(startDate));

Output:

    172800000

    testScript(); 
    function testScript() { 
    var now = new GlideDateTime(); //current date and time
    var sched = new GlideSchedule("<sys_id>"); // Use a cmn_schedule sys_id 
    if (sched.isInSchedule(now)){ 
    gs.info('We are in an active schedule window so whenNext() is not helpful'); 
    } else{  
    gs.info('Not currently in schedule so call whenNext()'); 
    var msUntilNext = sched.whenNext(new GlideDateTime(), 'US/Pacific'); 
    gs.info('Next schedule starts in '+msUntilNext+' milliseconds'); 
    } 
    }
    \\ Output [schedule inactive)]:
    \\ *** Script: Not currently in schedule so call whenNext() 
    \\ *** Script: Next schedule starts in -1 milliseconds

Output:

    [Scheduled for future] *** Script: Not currently in schedule *** Script: Next schedule starts in 332894000 milliseconds


