---
sourceDocument: Xanadu API リファレンス
sourceDocumentLink: https://servicenow-prod.fluidtopics.net/r/ja-JP/xanadu/api-reference

 Release :

    - xanadu

ft:locale :

    - ja-JP

ft:publication_title :

    - Xanadu API リファレンス

ft:clusterId :

    - crapiref

bundleId :

    - crapiref

workflow :

    - Creator


---

# 単純な期間と相対期間

# 単純な期間と相対期間 {#ariaid-title1}

* リリースバージョン: Xanadu
* 
* 更新日 2024年08月01日
* 
* ![](https://www.servicenow.com/docs/portal-asset/ico-clock) 所要時間：8分

タスクを完了するために必要な作業量を「相対期間」で表すことができます。

相対期間では、開始時間との相対関係で予想できる期限となる日時が決定されます。相対期間の例としては、「翌営業日の午後 4 時まで」や「2 営業日後の午前 10 時 30 分まで」などがあります。  
相対期間を計算するには、「翌営業日」が何を意味するかを判断するために、カレンダーとタイムゾーンを考慮する必要があります。これは、有効な営業日とはいつであるかを定義しているのがカレンダーであり、タイムゾーンも結果に影響するためです。例として、「翌営業日の 4 pm まで」を考えてみます。

* 開始が月曜日の 12 pm の場合：翌営業日の 4 pm =\> 火曜日の 4 pm
* 開始が金曜日の 2 pm の場合：翌営業日の 4 pm =\> 次の月曜日の 4 pm
{#c_SImpleDurationVsRelativeDuration__ul_jr2_s5t_1q}  
注:  
多くの場合、「翌営業日」は開始日時によって定義されます。たとえば、「2 pm より前であれば翌営業日の 4 pm」は、現在の時刻が営業日の 2 pm を過ぎていると、実際にはその日から 2 営業日後が期日になることを意味します。

## 単純な期間の計算 {#ariaid-title2}

このビジネスルールとスクリプトの例は、単純な期間を計算する方法を示しています。  

    var dur =new DurationCalculator();
    dur.setSchedule(current.schedule);
    dur.setStartDateTime("");
     
    if(current.duration_type==""){
             dur.calcDuration(current.duration.getGlideObject().getNumericValue()/1000);}else{
             dur.calcRelativeDuration(current.duration_type);}
     
        current.end_date_time= dur.getEndDateTime();
        current.work_seconds= dur.getSeconds();

このスクリプトは、DurationCalculator を使用して単純な期間を計算する方法を示しています。  

    /**
     * Sample script demonstrating use of DurationCalculator to compute simple durations
     * 
     */
     
    gs.include('DurationCalculator');
    executeSample();
     
    /**
     * Function to house the sample script.
     */
    function executeSample(){
     
        // First we need a DurationCalculator object.
        var dc =new DurationCalculator();
     
        // Compute a simple duration without any schedule. The arguments
        // can also be of type GlideDateTime, such as fields from a GlideRecord.
        var dur = dc.calcScheduleDuration("5/1/2012","5/2/2012");
        gs.log("calcScheduleDuration no schedule: "+ dur);
        // 86400 seconds (24 hours)
     
        // The above sample is useful in limited cases. We almost always want to 
        // use some schedule in a duration computation, let's load a schedule.
        addSchedule(dc);
     
        // Compute a duration using the schedule. The schedule
        // specifies a nine hour work day. The output of this is 32400 seconds, or
        // a nine hour span.
        dur = dc.calcScheduleDuration("5/23/2012 12:00","5/24/2012 12:00");
        gs.log("calcScheduleDuration with schedule: "+ dur);
        // 32400 seconds (9 hours)
     
        // Compute a duration that spans a weekend and holiday. Even though this
        // spans three days, it only spans 9 work hours based on the schedule.
        dur = dc.calcScheduleDuration("5/25/2012 12:00","5/29/2012 12:00");
        gs.log("calcScheduleDuration with schedule spaning holiday: "+ dur);
        // 32400 seconds (9 hours)
     
        // Use the current date time in a calculation. The output of this is
        // dependent on when you run it.
        var now =new Date();
        dur = dc.calcScheduleDuration("5/15/2012",new GlideDateTime());
        gs.log("calcScheduleDuration with schedule to now: "+ dur);
        // Different on every run.}
     
    /** 
     * Add a specific schedule to the DurationCalculator object.
     *  
     * @param durationCalculator An instance of DurationCalculator
     */
    function addSchedule(durationCalculator){
       //  Load the "8-5 weekdays excluding holidays" schedule into our duration calculator.
       var scheduleName ="8-5 weekdays excluding holidays";
       var grSched =new GlideRecord('cmn_schedule');
       grSched.addQuery('name', scheduleName);
       grSched.query();if(!grSched.next()){
            gs.log('*** Could not find schedule "'+ scheduleName +'"');
            return;}
        durationCalculator.setSchedule(grSched.getUniqueValue());}

## 相対期間の計算 {#ariaid-title3}

相対期間計算スクリプトの例。  
このスクリプトは、「10 am を過ぎていれば翌日 4 pm」の相対期間を計算します。

    // Next day at 4pm if before 10am
    var days =1;
    if(calculator.isAfter(calculator.startDateTime,"10:00:00")) 
          days++;
     
    calculator.calcRelativeDueDate(calculator.startDateTime, days,"16:00:00");

このスクリプトは、DurationCalculator を使用して相対期間を計算する方法を示しています。  

    /**
     * Sample use of relative duration calculation.
     * 
     */
     
    gs.include('DurationCalculator');
    executeSample();
     
    /**
     * Function to house the sample script.
     */
    function executeSample(){
     
        // First we need a DurationCalculator object. We will also use
        // the out-of-box relative duration "2 bus days by 4pm"
        var dc =new DurationCalculator();
        var relDur ="3bf802c20a0a0b52008e2859cd8abcf2";
        // 2 bus days by 4pm if before 10am
        addSchedule(dc);
     
        // Since our start date is before 10:00am our result is two days from
        // now at 4:00pm.
        dc.setStartDateTime("5/1/2012 09:00:00");
        if(!dc.calcRelativeDuration(relDur)){
            gs.log("*** calcRelativeDuration failed");
            return;}
        gs.log("Two days later 4:00pm: "+ dc.getEndDateTime());
     
        // Since our start date is after 10:00am our result is three days from
        // now at 4:00pm.
        dc.setStartDateTime("5/1/2012 11:00:00");
        if(!dc.calcRelativeDuration(relDur)){
            gs.log("*** calcRelativeDuration failed");
            return;}
        gs.log("Three days later 4:00pm: "+ dc.getEndDateTime());}
     
    /** 
     * Add a specific schedule to the DurationCalculator object.
     *  
     * @param durationCalculator An instance of DurationCalculator
     */
    function addSchedule(durationCalculator){
      //  Load the "8-5 weekdays excluding holidays" schedule into our duration calculator.
      var scheduleName ="8-5 weekdays excluding holidays";
      var grSched =new GlideRecord('cmn_schedule');
      grSched.addQuery('name', scheduleName);
      grSched.query();
      if(!grSched.next()){
            gs.log('*** Could not find schedule "'+ scheduleName +'"');
            return;}
      durationCalculator.setSchedule(grSched.getUniqueValue(),"GMT");}


