2.0.5 • Published 2 years ago

@kinaxis/calendar-math v2.0.5

Weekly downloads
-
License
Proprietary. See ...
Repository
-
Last release
2 years ago

calendar-math

Helper functions for manipulating dates using RapidResponse calendars.

Documentation

Types

Calendar

A collection of dates which define the intervals of the calendar. Example RapidResponse calendars are Week, Month, Workday, and Everyday.

PropertyTypeDescription
DatesCalendarDate[]Dates in the calendar, each representing the beginning of intervals. For example, the month calendar contains a CalendarDate for the first day of each month.

CalendarDate

A date in a Calendar, representing the start of an interval.

PropertyTypeDescription
ValueDateDate value

To use RapidResponse calendars in an embedded algorithm, the algorithm must have appropriate dependencies on the Calendar and CalendarDate tables.

For more information on RapidResponse calendars, see https://help.kinaxis.com/20162/GlobalHelp/Content/RR_DataModel/Calendar_and_date_calcs/Calendar_tables.htm.

Functions

addCalendarToDate(date, amount[, calendar])

Offset a date by the specified number of calendar intervals, according to RapidResponse date arithmetic rules.

For example, to return the first day of the next week you would use the Week calendar and specify amount as 1. To return the first day of the previous week you would use the Week calendar and specify amount as -1. Adding 0 returns the beginning of the the specified date's current interval for the specified calendar. For example, adding 0 using the Week calendar returns the first day of the date's week. Doing the same with the Month calendar returns the first day of the date's month.

ArgumentTypeDescription
dateDateThe date to offset. Date.Past and Date.Future are sticky, meaning that any addition on them results in the same date. Using an invalid date will return Date.Undefined.
amountnumberThe number of calendar integer intervals to offset date by
calendarCalendarOptional calendar to use for the calculation. If no calendar is specified or the calendar has no dates, amount is interpreted in days. If the calendar only has one date, then any result less than that date returns Date.Past and any result greater than or equal to that date returns Date.Future.

Return value: The Date obtained by adding amount intervals in the specified calendar to date. If the result is less than the first valid date in the calendar, Date.Past is returned. If the result is greater than the last valid date in the calendar, Date.Future is returned.

CAUTION: When using a date constant (i.e., Date.PAST, Date.FUTURE, or Date.UNDEFINED), do so using the globally available date constants instead of manually creating the dates. Future versions of the library or the embedded algorithms runtime may change the values of these date constants.

CAUTION: Do not modify calendar in code after calling this function. For performance reasons, data structures based on the data in calendar are generated and cached internally. Calling this function with the same Calendar object will use the same internal cache regardless of whether or not the contents of that object have changed. This will lead to incorrect results if it is modified. To modify calendar data, do so in RapidResponse.


subtractDates(date1, date2[, calendar])

Calculate the number of intervals in calendar between date1 and date2, according to RapidResponse date arithmetic rules.

For example, new Date("2020-09-14T00:00:00.000Z") - new Date("2020-06-12T00:00:00.000Z") using the month calendar returns 3.

ArgumentTypeDescription
date1DateThe first date
date2DateThe second date
calendarCalendarOptional calendar to use for the calculation. If no calendar is specified or the calendar has no dates then result will be in days. If the calendar only has one date, the result will be 65535.

Return value: The span of time between date1 and date2 in intervals of calendar. If date1 or date2 are invalid, equal to Date.Past, Date.Future, or Date.Undefined, or if the result of the calculation is outside of the bounds of the specified calendar then the result will be 65535 (if date1 is larger) or -65535 (if date2 is larger). If the calendar only has one date, then the result will always be 65535.

CAUTION: When using a date constant (i.e., Date.PAST, Date.FUTURE, or Date.UNDEFINED), do so using the globally available date constants instead of manually creating the dates. Future versions of the library or the embedded algorithms runtime may change the values of these date constants.

CAUTION: Do not modify calendar in code after calling this function. For performance reasons, data structures based on the data in calendar are generated and cached internally. Calling this function with the same Calendar object will use the same internal cache regardless of whether or not the contents of that object have changed. This will lead to incorrect results if it is modified. To modify calendar data, do so in RapidResponse.


For more information on RapidResponse date arithmetic rules, see https://help.kinaxis.com/20162/GlobalHelp/Content/RR_DataModel/Calendar_and_date_calcs/Date_calculation_rules.htm.

Please contact support@kinaxis.com if you experience any issues.

Copyright © 2022 Kinaxis. All Rights Reserved.