ServiceNow提供的官方scheduleAPI 中有一个add方式。
Scoped GlideSchedule - add(GlideDateTime startDate, GlideDuration offSet)

*parameter1:
Name:startDate
Type:GlideDateTime
Description:The starting date of the new schedule segment.

parameter2:
Name:offSet
Type:GlideDuration
Description:The time offset of the new schedule segment.*

example:

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

然则 这个方式中parameter2 duration不能小于零。以是行使该方式我们只能求schedule中未来的日期,无法往前推算日期。

name问题来了,当我们想得到schedule中某一特定日期前3天(schedule里的3天而非calendar的3天)的日期应该怎么办呢?

ok下面上干货。

思绪

1 GlideDateTime.addDaysLocalTime() 参数可以为负数;
2 简朴的行使一个for循环;

/**
endTime is string
workDay type is int
**/
this._schedule = ‘*******************’;
getWorkDateInSchedule: function(endTime,workDay){   
var inter = 1;
if(workDay<0){
//workDay <0 特定日期往前推
    inter = -1;
    var workDayABS = Math.abs(workDay);
    var gt_end = new GlideDateTime(endTime);
    var sched = new GlideSchedule(this._schedule);
    var startWorkDate;
    var inte = 0;
    for(var i = 0; ; i++){
        //GlideDateTime.addDaysLocalTime() 参数可以为负数;
        gt_end.addDaysLocalTime(inter);
            if(sched.isInSchedule(gt_end)){
            inte = inte + 1;
        }
        if(inte == workDayABS){
            break;
        }
    }
    startWorkDate = gt_end;
    return startWorkDate.getDate().toString();
}
else{
// workDay > 0 特定日期往后推时就挪用官方api
    var schedule = new GlideSchedule(this._schedule);
    var end = schedule.add(endTime, dur);
    return = end.getDate().toString();
}
},