Schedule 方法求向前向後的日期----- 關於GlideSchedule.add的拓展

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

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

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

example:code

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中將來的日期,沒法往前推算日期。ip

name問題來了,當咱們想獲得schedule中某一特定日期前3天(schedule裏的3天而非calendar的3天)的日期應該怎麼辦呢?get

ok下面上乾貨。string

思路it

1 GlideDateTime.addDaysLocalTime() 參數能夠爲負數;
2 簡單的利用一個for循環;io

/**
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();
}
},
相關文章
相關標籤/搜索