yanweiyuan3
2023-08-09 588bc7829387dfc761cc25f06f77d4c81818bd10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Quintiq file version 2.0
#parent: #root
StaticMethod GetStartOfNextMonth (
  DateTime datetime,
  Number nrofrecurrent
) const declarative remote as DateTime
{
  Description:
  [*
    Return the start date of the next month for the given date and number of recurrent in month.
    This method will return a exact date after a month from the date, e.g Given 15 Jan, this will return 15 Feb.
  *]
  TextBody:
  [*
    /* Only add the durationfromstart after loop to avoid exceeding the last day of next month:
       eg. Adding 2 months to 31-Jan using GetStartOfNextMonth will return
           the same day of targetted month or last day of month ie. 31-Mar
    */
    newstartofmonth := datetime;
    durationfromstart := datetime - datetime.StartOfMonth();
    
    // durationinday will retain the time of the day
    durationinday := datetime - datetime.StartOfDay();
    
    for( reccurent := 1; reccurent <= nrofrecurrent; reccurent++ )
    {
      newstartofmonth := newstartofmonth.StartOfNextMonth();
    }
    
    /* Add durationinday to last day of the new month to avoid losing the 
       time of the day                                     */                             
    lastdayofnewmonth := newstartofmonth.StartOfNextMonth() - Duration::Days( 1 ) + durationinday;
    newdate := minvalue( newstartofmonth + durationfromstart, lastdayofnewmonth );
    
    return newdate
  *]
}