Namespace: calc

calc

Functions to calculate new date or related values.
Source:

Methods

(static) calcDateByDayOfYear(day, year) → {Object|null}

Calculates the date according to a day in the sequence of a year (from 1 to 365/366).
Parameters:
Name Type Description
day number A day
year number A year
Source:
Returns:
An object expressing a date ({ year:, month:, day: }) or null in case of invalid params
Type
Object | null
Example
calcDateByDayOfYear( 32, 2021 ); // returns { year: 2021, month: 2, day: 1 }
calcDateByDayOfYear( 365, 2021 ); // returns { year: 2021, month: 12, day: 31 }
calcDateByDayOfYear(); // returns null

- - -

(static) calcDateByDaysAddition(from, days) → {Array.<Object>|null}

Calculates the date according to a starting date and a positive or negative number of days to be added.
Parameters:
Name Type Description
from Object | Array.<Object> | string | Date A starting date
days number A number of days to be added
Source:
Returns:
An array expressing a date ([ year, month, day ]) or null in case of invalid params
Type
Array.<Object> | null
Example
calcDateByDaysAddition( "20210110", 10 ); // returns [ 2021, 1, 20 ]
calcDateByDaysAddition( "20210110", -10 ); // returns [ 2020, 12, 31 ]
calcDateByDaysAddition( "20210110" ); // returns null

- - -

(static) calcDayOfWeek(date) → {number|null}

Calculates the position of a date in the days sequence of the week (from 1 to 7, monday to sunday).
Parameters:
Name Type Description
date Object | Array.<Object> | string | Date A date value
Source:
Returns:
The position in week or null in case of invalid param
Type
number | null
Example
calcDayOfWeek( "20210507" ); // returns 5
calcDayOfWeek(); // returns null

- - -

(static) calcDayOfYear(date) → {number|null}

Calculates the position of a date in the days sequence of the year (from 1 to 365/366).
Parameters:
Name Type Description
date Object | Array.<Object> | string | Date A date value
Source:
Returns:
The position in year or null in case of invalid param
Type
number | null
Example
calcDayOfYear( "20210201" ); // returns 32
calcDayOfYear( "20211231" ); // returns 365
calcDayOfYear(); // returns null

- - -

(static) calcWeeksOfRange(from, till) → {Array.<Object>|null}

Calculates the weeks according to a range of days that are placed within.
Parameters:
Name Type Description
from Object | Array.<Object> | string | Date A starting date of a range
till Object | Array.<Object> | string | Date An ending date of a range
Source:
Returns:
A 2-dimensional array with the starting & ending dates of each week or null in case of invalid params
Type
Array.<Object> | null
Example
calcWeeksOfRange( '20210501', '20210515' ) 
     // returns [
     //   [ '20210426', '20210502' ],
     //   [ '20210503', '20210509' ],
     //   [ '20210510', '20210516' ]
     // ]
calcWeeksOfRange( '20210501' ); // returns null 

- - -