DateHelper
A static class offering date manipulation, comparison, parsing and formatting helper methods.
Parsing strings
Use DateHelper.parse() to parse strings into dates. It accepts a date string and a format specifier.
The format specifier is string built up using the following tokens:
| Unit | Token | Description |
|---|---|---|
| Year | YYYY | 4-digits year, like: 2018 |
| Y | numeric, any number of digits | |
| YY | < 68 -> 2000, > 68 -> 1900 | |
| Month | MM | 01 - 12 |
| Month | MMM | Short name of the month |
| Date | DD | 01 - 31 |
| Hour | HH | 00 - 23 or 1 - 12 |
| Minute | mm | 00 - 59 |
| Second | ss | 00 - 59 |
| Millisecond | S | 0 - 9 [000, 100, 200 .. 900 ] |
| SS | 00 - 99 [000, 010, 020 .. 990 ] | |
| SSS | 000 - 999 [000, 001, 002 .. 999 ] | |
| AM/PM | A | AM or PM |
| a | am or pm | |
| TimeZone | Z | Z for UTC or +-HH:mm. Local timezone if left out |
| Predefined | L | Long date, MM/DD/YYYY |
| LT | Long time, HH:mm A |
Default parse format is: 'YYYY-MM-DDTHH:mm:ss.SSSZ' see defaultParseFormat.
For example:
DateHelper.parse('2018-11-06', 'YYYY-MM-DD');
DateHelper.parse('13:14', 'HH:mm');
DateHelper.parse('6/11/18', 'DD/MM/YY');
Formatting dates
Use DateHelper.format() to create a string from a date using a format specifier. The format specifier is similar to
that used when parsing strings. It can use the following tokens (input used for output below is
new Date(2018,8,9,18,7,8,145)):
| Unit | Token | Description & output |
|---|---|---|
| Year | YYYY | 2018 |
| YY | 18 | |
| Y | 2018 | |
| Quarter | Q | 3 |
| Qo | 3rd | |
| Month | MMMM | September |
| MMM | Sep | |
| MM | 09 | |
| Mo | 9th | |
| M | 9 | |
| Week (iso) | WW | 37 (2 digit, zero padded) |
| Wo | 37th | |
| W | 37 | |
| WWp | Week 37 (localized prefix, zero pad) | |
| Wp | Week 37 (localized prefix) | |
| WWp0 | W37 (localized prefix) | |
| Wp0 | W37 (localized prefix) | |
| Date | DDDD | Day of year, 3 digits |
| DDDo | Day of year, ordinal | |
| DDD | Day of year | |
| DD | 09 | |
| Do | 9th | |
| D | 9 | |
| Weekday | dddd | Sunday |
| ddd | Sun | |
| dd | Su | |
| d1 | S | |
| do | 0th | |
| d | 0 | |
| Hour | HH | 18 (00 - 23) |
| H | 18 (0 - 23) | |
| hh | 06 (00 - 12) | |
| h | 6 (0 - 12) | |
| KK | 19 (01 - 24) | |
| K | 19 (1 - 24) | |
| kk | 06 or 18, locale determines | |
| k | 6 or 18, locale determines | |
| Minute | mm | 07 |
| m | 7 | |
| Second | ss | 08 |
| s | 8 | |
| Millisecond | S | 1 (100ms) |
| SS | 14 (140ms) | |
| SSS | 145 (145ms) | |
| AM/PM | A | AM or PM |
| a | am or pm | |
| Predefined | LT | H: 2-digit (2d), m: 2d |
| (uses browser locale) | LTS | H: 2d, m: 2d, s : 2d |
| LST | Depends on 12 or 24 hour clock | |
| 12h, H : 1d, m : 0 or 2d | ||
| 24h, H : 2d, m : 2d | ||
| L | Y: numeric (n), M : 2d, D : 2d | |
| l | Y: n, M : n, D : n | |
| LL | Y: n, M : long (l), D : n | |
| ll | Y: n, M : short (s), D : n | |
| LLL | Y: n, M : l, D : n, H: n, m: 2d | |
| lll | Y: n, M : s, D : n, H: n, m: 2d | |
| LLLL | Y: n, M : l, D : n, H: n, m: 2d, d: l | |
| llll | Y: n, M : s, D : n, H: n, m: 2d, d: s | |
| u | YYYYMMDDZ in UTC zone | |
| uu | YYYYMMDDTHHMMSSZ in UTC zone |
Default format is: 'YYYY-MM-DDTHH:mm:ssZ' see defaultFormat
For example:
DateHelper.format(new Date(2018,10,6), 'YYYY-MM-DD'); // 2018-11-06
DateHelper.format(new Date(2018,10,6), 'M/D/YY'); // 11/6/18
Using embedded text inside format string
Arbitrary text can be embedded in the format string by wrapping it with {}:
DateHelper.format(new Date(2019, 7, 16), '{It is }dddd{, yay!}') -> It is Friday, yay!
Overriding Formatters
To override formatters, you need to configure the locale.
Here is a sample code snippet demonstrating how to override formatters using a custom locale.
const customLocale = {
DateHelper : {
formats : {
ll : date => {
return DateHelper.format(date, 'DD.MM.YYYY');
},
L : date => {
return DateHelper.format(date, 'DD--MM--YYYY')
},
LT : date => {
return DateHelper.format(date, 'HH-mm');
},
LTS : date => {
return DateHelper.format(date, 'HH-mm-ss');
}
}
}
};
// Extend the English locale with the custom locale
LocaleHelper.publishLocale('En', locale);
Once the custom locale is published, you can use the overridden formatters with DateHelper.format().
DateHelper.format(new Date(2024, 0, 12, 12, 4, 5), 'll') // Output: 12.01.2024
DateHelper.format(new Date(2024, 0, 12, 12, 4, 5), 'L') // Output: 12--01--2024
DateHelper.format(new Date(2024, 0, 12, 12, 4, 5), 'LT') // Output: 12-04
DateHelper.format(new Date(2024, 0, 12, 12, 4, 5), 'LTS') // Output: 12-04-05
Unit names
Many DateHelper functions (for example add, as, set) accepts a unit among their params. The following units are available:
| Unit | Aliases |
|---|---|
| millisecond | millisecond, milliseconds, ms |
| second | second, seconds, s |
| minute | minute, minutes, m |
| hour | hour, hours, h |
| day | day, days, d |
| week | week, weeks, w |
| month | month, months, mon, mo, M |
| quarter | quarter, quarters, q |
| year | year, years, y |
| decade | decade, decades, dec |
For example:
DateHelper.add(date, 2, 'days');
DateHelper.as('hour', 7200, 'seconds');
Properties
6
Properties
6Get the local AM indicator string.
Get/set the default format used by format() and parse(). Defaults to 'YYYY-MM-DDTHH:mm:ssZ'
(~ISO 8601 Date and time, '1962-06-17T09:21:34Z').
Get/set the default format used by parse(). Defaults to 'YYYY-MM-DDTHH:mm:ss.SSSZ' or defaultFormat
(~ISO 8601 Date and time, '1962-06-17T09:21:34.123Z').
Get non-working days as an object where keys are day indices, 0-6 (Sunday-Saturday), and the value is true.
This is determined by the current locale's DateHelper.nonWorkingDays parameter.
For example:
{
0 : true, // Sunday
6 : true // Saturday
}
Get the local PM indicator string.
Get the first day of week, 0-6 (Sunday-Saturday).
This is determined by the current locale's DateHelper.weekStartDay parameter.
Functions
56
Functions
56Comparison
Checks if this date is >= start and < end.
| Parameter | Type | Description |
|---|---|---|
date | Date | The source date |
start | Date | Start date |
end | Date | End date |
true if this date falls on or between the given start and end dates
Checks if this date is >= start and <= end.
| Parameter | Type | Description |
|---|---|---|
date | Date | The source date |
start | Date | Start date |
end | Date | End date |
true if this date falls on or between the given start and end dates
Compares two dates using the specified precision.
| Parameter | Type | Description |
|---|---|---|
first | Date | First date |
second | Date | Second date |
unit | String | Unit to calculate difference in. If not given, the comparison will be done up to a millisecond |
0 = equal, -1 = first before second, 1 = first after second
Compare two units. Returns 1 if first param is a greater unit than second param, -1 if the opposite is true or 0 if they're equal.
| Parameter | Type | Description |
|---|---|---|
unit1 | DurationUnit | The 1st unit |
unit2 | DurationUnit | The 2nd unit |
Returns 1 if first param is a greater unit than second param, -1 if the opposite is true or 0 if they're equal
Returns true if date ranges intersect.
| Parameter | Type | Description |
|---|---|---|
date1Start | Date | Start date of first span |
date1End | Date | End date of first span |
date2Start | Date | Start date of second span |
date2End | Date | End date of second span |
Returns true if dates intersect
Determines if a date succeeds another.
| Parameter | Type | Description |
|---|---|---|
first | Date | First date |
second | Date | Second date |
true if first succeeds second, otherwise false
Determines if a date precedes another.
| Parameter | Type | Description |
|---|---|---|
first | Date | First date |
second | Date | Second date |
true if first precedes second, otherwise false
Checks if two dates are equal.
| Parameter | Type | Description |
|---|---|---|
first | Date | First date |
second | Date | Second date |
unit | String | Unit to calculate difference in. If not given, the comparison will be done up to a millisecond |
true if the dates are equal
Checks if date is the start of specified unit.
| Parameter | Type | Description |
|---|---|---|
date | Date | Date |
unit | DurationUnit | Time unit |
true if date is the start of specified unit
Determines if a date is today's date.
| Parameter | Type | Description |
|---|---|---|
date | Date | The date to check |
timeZone | String | The time zone to use for the comparison. If not specified, the local time zone is used: |
true if the date is today, otherwise false
Returns true if the first time span completely 'covers' the second time span.
DateHelper.timeSpanContains(
new Date(2010, 1, 2),
new Date(2010, 1, 5),
new Date(2010, 1, 3),
new Date(2010, 1, 4)
) ==> true
DateHelper.timeSpanContains(
new Date(2010, 1, 2),
new Date(2010, 1, 5),
new Date(2010, 1, 3),
new Date(2010, 1, 6)
) ==> false
| Parameter | Type | Description |
|---|---|---|
spanStart | Date | The start date for initial time span |
spanEnd | Date | The end date for initial time span |
otherSpanStart | Date | The start date for the 2nd time span |
otherSpanEnd | Date | The end date for the 2nd time span |
true if the first time span completely 'covers' the second time span
Manipulate
Add days, hours etc. to a date. Always clones the date, original will be left unaffected.
| Parameter | Type | Description |
|---|---|---|
date | Date | String | Original date |
amount | Number | String | Duration | DurationConfig | Amount of days, hours etc. or a string representation of a duration
as accepted by parseDuration or an object with |
unit | DurationUnitShort | Unit for amount |
New calculated date
Removes time from a date (same as calling startOf(date)).
| Parameter | Type | Description |
|---|---|---|
date | Date | Date to remove time from |
clone | Boolean | Manipulate a copy of the date |
Manipulated date
Creates a clone of the specified date
| Parameter | Type | Description |
|---|---|---|
date | Date | Original date |
Cloned date
Constrains the date within a min and a max date.
| Parameter | Type | Description |
|---|---|---|
date | Date | The date to constrain |
min | Date | Min date |
max | Date | Max date |
The constrained date
Copies hours, minutes, seconds, milliseconds from one date to another.
| Parameter | Type | Description |
|---|---|---|
targetDate | Date | The target date |
sourceDate | Date | The source date |
The adjusted target date
Calculates the difference between two dates, in the specified unit.
| Parameter | Type | Description |
|---|---|---|
start | Date | First date |
end | Date | Second date |
unit | DurationUnitShort | Unit to calculate difference in |
fractional | Boolean | Specify false to round result |
Difference in the specified unit
Returns time with default year, month, and day (Jan 1, 2020).
| Parameter | Type | Description |
|---|---|---|
hours | Number | Date | Hours value or the full date to extract the time of |
minutes | Number | Minutes value |
seconds | Number | Seconds value |
ms | Number | Milliseconds value |
A new default date with the time extracted from the given date or from the time values provided individually
Returns the elapsed milliseconds from the start of the specified date.
| Parameter | Type | Description |
|---|---|---|
date | Date | Date to remove date from |
unit | DurationUnitShort | The time unit to return |
The elapsed milliseconds from the start of the specified date
Sets a part of a date (in place).
| Parameter | Type | Description |
|---|---|---|
date | Date | Date to manipulate |
unit | String | Object | Part of date to set, for example |
amount | Number | Value to set |
Passed date instance modified according to the arguments
Sets the date to the start of the specified unit, by default returning a clone of the date instead of changing it in place.
| Parameter | Type | Description |
|---|---|---|
date | Date | Original date |
unit | String | Start of this unit, |
clone | Boolean | Manipulate a copy of the date |
weekStartDay | Number | The first day of week, |
Manipulated date
Other
Converts the passed Date to an accurate number of months passed since the epoch start.
| Parameter | Type | Description |
|---|---|---|
time | Date | The Date to find the month value of |
The number of months since the system time epoch start. May be a fractional value
Ceils the passed Date value to the nearest increment value.
Optionally may ceil relative to a certain base time point.
For example DH.ceil(new Date('2020-01-01T09:35'), '30 min', new Date('2020-01-01T09:15'))
would ceil to 9:45 because that's the closest higher integer number of 30 minute increments
from the base.
Note that base is ignored when ceiling to weeks. Use weekStartDay argument which default to the configured
weekStartDay dictates what the base of a week is
| Parameter | Type | Description |
|---|---|---|
time | Date | The time to ceil |
increment | String | Number | DurationConfig | Object | A numeric millisecond value by which to ceil the time
or a duration in string form eg |
base | Date | The start from which to apply the ceiling |
weekStartDay | Number | Will default to what is set in locale |
New Date instance
Coerces the passed Date between the passed minimum and maximum values.
| Parameter | Type | Description |
|---|---|---|
date | Date | The date to clamp between the |
min | Date | The minimum Date |
max | Date | The maximum Date |
If the passed date is valid, a new Date object which is clamped between the min and max
Returns the end point of the passed date, that is 00:00:00 of the day after the passed date.
| Parameter | Type | Description |
|---|---|---|
date | Date | The date to return the end point of |
Manipulated date
Floor the passed Date value to the nearest increment value.
Optionally may floor relative to a certain base time point.
For example DH.floor(new Date('2020-01-01T09:35'), '30 min', new Date('2020-01-01T09:15'))
would floor to 9:15 because that's the closest lower integer number of 30 minute increments
from the base.
Note that base is ignored when flooring to weeks. The configured weekStartDay
dictates what the base of a week is.
| Parameter | Type | Description |
|---|---|---|
time | Date | The time to floor |
increment | String | Number | DurationConfig | Object | A numeric millisecond value by which to floor the time.
or a duration in string form eg |
base | Date | The start from which to apply the flooring |
weekStartDay | Number | Will default to what is set in locale |
New Date instance
Formats a range of dates using the specified format. Because two dates are involved, the format specifier
uses the tokens S{} and E{}. The text contained between the {} is the format
for the start date or end date, respectively. Text not inside these tokens is retained verbatim.
For example:
DateHelper.formatRange(dates, 'S{DD MMM YYYY} - E{DD MMM YYYY}');
The above will format dates[0] based on the S{DD MMM YYYY} segment and dates[1] using E{DD MMM YYYY}. The ' - '` between these will remain between the two formatted dates.
| Parameter | Type | Description |
|---|---|---|
dates | Date[] | An array of start date and end date ( |
format | String | The format specifier |
Converts a millisecond time delta to an object structure. For example 1000 * 60 * 60 * 50
milliseconds the result would be as:
{
day : 2,
hour : 2
}
| Parameter | Type | Description |
|---|---|---|
delta | Number | The millisecond delta value |
options | Object | Formatting options |
options.abbrev | Boolean | Pass |
options.precision | String | The minimum precision unit |
options.ignoreLocale | Boolean | Pass true to return unlocalized unit name. Requires |
options.maxUnit | String | Name of the maximum unit in the output. e.g. if you pass |
The object with the values for each unit
Checks if value is a date object. Allows to recognize date object even from another context, like the top frame when used in an iframe.
| Parameter | Type | Description |
|---|---|---|
value | * | Value to check |
true if value is a date object
Checks if date object is valid.
For example:
date = new Date('foo')
date instanceof Date // true
date.toString() // Invalid Date
isNaN(date) // true
DateHelper.isValidDate(date) // false
date = new Date()
date instanceof Date // true
date.toString() // Mon Jan 13 2020 18:27:38 GMT+0300 (GMT+03:00)
isNaN(date) // false
DateHelper.isValidDate(date) // true
| Parameter | Type | Description |
|---|---|---|
date | Date | Date |
true if date object is valid
Rounds the passed Date value to the nearest increment value.
Optionally may round relative to a certain base time point.
For example DH.round(new Date('2020-01-01T09:35'), '30 min', new Date('2020-01-01T09:15'))
would round to 9:45 because that's the nearest integer number of 30 minute increments
from the base.
Note that base is ignored when rounding to weeks. The configured weekStartDay
dictates what the base of a week is.
| Parameter | Type | Description |
|---|---|---|
time | Date | The time to round |
increment | String | Number | A millisecond value by which to round the time
May be specified in string form eg: |
base | Date | The start from which to apply the rounding |
weekStartDay | Number | Will default to what is set in locale |
New Date instance
Parse & format
Converts the specified amount of one unit (fromUnit) into an amount of another unit (toUnit).
| Parameter | Type | Description |
|---|---|---|
toUnit | String | The name of units to convert to, eg: |
amount | Number | String | The time to convert. Either the magnitude number form or a duration string such as '2d' |
fromUnit | String | If the amount was passed as a number, the units to use to convert from |
Converts the specified amount of desired unit into milliseconds. Can be called by only specifying a unit as the
first argument, it then uses amount = 1.
For example:
asMilliseconds('hour') == asMilliseconds(1, 'hour')
| Parameter | Type | Description |
|---|---|---|
amount | Number | String | Amount, what of is decided by specifying unit (also takes a unit which implies an amount of 1) |
unit | String | Time unit (s, hour, months etc.) |
Creates a date from a date definition object. The object can have the following properties:
- year
- month
- date (day in month)
- hours
- minutes
- seconds
- milliseconds
- amPm : 'am' or 'pm', implies 12-hour clock
- timeZone : offset from UTC in minutes
| Parameter | Type | Description |
|---|---|---|
definition | Object | |
definition.year | Number | |
definition.month | Number | |
definition.date | Number | |
definition.hours | Number | |
definition.minutes | Number | |
definition.seconds | Number | |
definition.milliseconds | Number | |
definition.amPm | Number | |
definition.timeZone | Number |
new Date instance
Converts a date to string with the specified format. Formats heavily inspired by https://momentjs.com.
Available formats (input used for output below is new Date(2018,8,9,18,7,8,145)):
| Unit | Token | Description & output |
|---|---|---|
| Decade | DC | 2010s |
| Year | YYYY | 2018 |
| YY | 18 | |
| Y | 2018 | |
| Quarter | Q | 3 |
| Qo | 3rd | |
| Month | MMMM | September |
| MMM | Sep | |
| MM | 09 | |
| Mo | 9th | |
| M | 9 | |
| Week (iso) | WW | 37 (2 digit, zero padded) |
| Wo | 37th | |
| W | 37 | |
| WWp | Week 37 (localized prefix, zero pad) | |
| Wp | Week 37 (localized prefix) | |
| WWp0 | W37 (localized prefix) | |
| Wp0 | W37 (localized prefix) | |
| Date | DDDD | Day of year, 3 digits |
| DDDo | Day of year, ordinal | |
| DDD | Day of year | |
| DD | 09 | |
| Do | 9th | |
| D | 9 | |
| Weekday | dddd | Sunday |
| ddd | Sun | |
| dd | Su | |
| d1 | S | |
| do | 0th | |
| d | 0 | |
| Hour | HH | 18 (00 - 23) |
| H | 18 (0 - 23) | |
| hh | 06 (00 - 12) | |
| h | 6 (0 - 12) | |
| KK | 19 (01 - 24) | |
| K | 19 (1 - 24) | |
| kk | 06 or 18, locale determines | |
| k | 6 or 18, locale determines | |
| Minute | mm | 07 |
| m | 7 | |
| Second | ss | 08 |
| s | 8 | |
| Millisecond | S | 1 (100ms) |
| SS | 14 (140ms) | |
| SSS | 145 (145ms) | |
| AM/PM | A | AM or PM |
| a | am or pm | |
| Predefined | LT | H: 2-digit (2d), m: 2d |
| (uses browser locale) | LTS | H: 2d, m: 2d, s : 2d |
| LST | Depends on 12 or 24 hour clock | |
| 12h, H : 1d, m : 0 or 2d | ||
| 24h, H : 2d, m : 2d | ||
| L | Y: numeric (n), M : 2d, D : 2d | |
| l | Y: n, M : n, D : n | |
| LL | Y: n, M : long (l), D : n | |
| ll | Y: n, M : short (s), D : n | |
| LLL | Y: n, M : l, D : n, H: n, m: 2d | |
| lll | Y: n, M : s, D : n, H: n, m: 2d | |
| LLLL | Y: n, M : l, D : n, H: n, m: 2d, d: l | |
| llll | Y: n, M : s, D : n, H: n, m: 2d, d: s |
Some examples:
DateHelper.format(new Date(2019, 7, 16), 'dddd') -> Friday
DateHelper.format(new Date(2019, 7, 16, 14, 27), 'HH:mm') --> 14:27
DateHelper.format(new Date(2019, 7, 16, 14, 27), 'L HH') --> 2019-07-16 14
Using embedded text inside format string
Arbitrary text can be embedded in the format string by wrapping it with {}:
DateHelper.format(new Date(2019, 7, 16), '{It is }dddd{, yay!}') -> It is Friday, yay!
| Parameter | Type | Description |
|---|---|---|
date | Date | Date |
format | String | function | Desired format (uses |
format.date | Date |
Formatted string
Converts a millisecond time delta to a human-readable form. For example 1000 * 60 * 60 * 50
milliseconds would be rendered as '2 days, 2 hours'.
| Parameter | Type | Description |
|---|---|---|
delta | Number | The millisecond delta value |
options | Object | Formatting options |
options.abbrev | Boolean | Pass |
options.precision | String | The minimum precision unit |
options.separator | String | The separator to use between magnitude and unit |
options.unitSeparator | String | The separator to use between each duration part, defaults to ", " eg. |
options.asString | Boolean | Pass |
Formatted string
Returns true for 24-hour format.
| Parameter | Type | Description |
|---|---|---|
format | String | Date format |
true for 24-hour format
Returns a date created from the supplied string using the specified format. Will try to create even if format
is left out, by first using the default format (see defaultFormat, by default
YYYY-MM-DDTHH:mm:ssZ) and then using new Date(dateString).
Supported tokens:
| Unit | Token | Description |
|---|---|---|
| Year | YYYY | 4-digits year, like: 2018 |
| Y | numeric, any number of digits | |
| YY | < 68 -> 2000, > 68 -> 1900 | |
| Month | MM | 01 - 12 |
| Month | MMM | Short name of the month |
| Date | DD | 01 - 31 |
| Hour | HH | 00 - 23 or 1 - 12 |
| Minute | mm | 00 - 59 |
| Second | ss | 00 - 59 |
| Millisecond | S | 0 - 9 [000, 100, 200 .. 900 ] |
| SS | 00 - 99 [000, 010, 020 .. 990 ] | |
| SSS | 000 - 999 [000, 001, 002 .. 999 ] | |
| AM/PM | A | AM or PM |
| a | am or pm | |
| TimeZone | Z | Z for UTC or +-HH:mm. Local timezone if left out |
| Predefined | L | Long date, MM/DD/YYYY |
| LT | Long time, HH:mm A |
Predefined formats and functions used to parse tokens can be localized, see for example the swedish locale
SvSE.js.
NOTE: If no date parameters are provided then Jan 01 2020 is used as a default date.
| Parameter | Type | Description |
|---|---|---|
dateString | String | Date string |
format | String | Date format (or defaultParseFormat if left out) |
new Date instance parsed from the string
Parses a typed duration value according to locale rules.
The value is taken to be a string consisting of the numeric magnitude and the units:
- The numeric magnitude can be either an integer or a float value. Both
','and'.'are valid decimal separators. - The units may be a recognised unit abbreviation of this locale or the full local unit name.
For example:
'2d', '2 d', '2 day', '2 days' will be turned into { magnitude : 2, unit : 'day' }
'2.5d', '2,5 d', '2.5 day', '2,5 days' will be turned into { magnitude : 2.5, unit : 'day' }
NOTE: Doesn't work with complex values like '2 days, 2 hours'
| Parameter | Type | Description |
|---|---|---|
value | String | The value to parse |
allowDecimals | Boolean | Decimals are allowed in the magnitude |
defaultUnit | DurationUnit | String | Default unit to use if only magnitude passed |
If successfully parsed, the result contains two properties, magnitude being a number, and
unit being the canonical unit name, NOT a localized name. If parsing was unsuccessful, null is returned
Parses a typed unit name, for example 'ms' or 'hr' or 'yr' into the
canonical form of the unit name which may be passed to add
or diff.
| Parameter | Type | Description |
|---|---|---|
unitName | DurationUnit | DurationUnitShort | String | Time unit name |
Query
Get number of days in the current month for the supplied date.
| Parameter | Type | Description |
|---|---|---|
date | Date | Date which month should be checked |
Days in month
Get the specified part of a date.
| Parameter | Type | Description |
|---|---|---|
date | Date | |
unit | DurationUnit | Part of date, hour, minute etc. |
The requested part of the specified date
Get the end of previous day.
| Parameter | Type | Description |
|---|---|---|
date | Date | Date |
noNeedToClearTime | Boolean | Flag to not clear time from the result |
New Date instance
Get the first date of the month for the supplied date.
| Parameter | Type | Description |
|---|---|---|
date | Date | Date |
New Date instance
Get the last date of the month for the supplied date.
| Parameter | Type | Description |
|---|---|---|
date | Date | Date |
New Date instance
Get an incremented date. Incrementation based on specified unit and optional amount.
| Parameter | Type | Description |
|---|---|---|
date | Date | Date |
unit | DurationUnit | Time unit |
increment | Number | Increment amount |
weekStartDay | Number | Will default to what is set in locale |
New Date instance
Get the start of the next day.
| Parameter | Type | Description |
|---|---|---|
date | Date | Date |
clone | Boolean | Clone date |
noNeedToClearTime | Boolean | Flag to not clear time from the result |
Passed Date or new Date instance, depending on the clone flag
Get week number for the date.
| Parameter | Type | Description |
|---|---|---|
date | Date | The date |
weekStartDay | Number | The first day of week, 0-6 (Sunday-Saturday). Defaults to the weekStartDay |
year and week number
Get the latest of two dates.
| Parameter | Type | Description |
|---|---|---|
first | Date | First date |
second | Date | Second date |
Latest date
Get the earliest of two dates.
| Parameter | Type | Description |
|---|---|---|
first | Date | First date |
second | Date | Second date |
Earliest date
Unit helpers
Turns (10, 'day') into '10 days' etc.
| Parameter | Type | Description |
|---|---|---|
count | Number | Amount of unit |
unit | DurationUnit | Unit, will be normalized (days, d -> day etc.) |
Amount formatted to string
Returns a duration of the timeframe in the given unit.
| Parameter | Type | Description |
|---|---|---|
start | Date | The start date of the timeframe |
end | Date | The end date of the timeframe |
unit | DurationUnit | Duration unit |
The duration in the units
Returns a localized full name of the duration unit.
For example in the EN locale, for 'd' it will return either
'day' or 'days', depending on the plural argument
Preserves casing of first letter.
| Parameter | Type | Description |
|---|---|---|
unit | DurationUnit | Time unit |
plural | Boolean | Whether to return a plural name or singular |
Localized full name of the duration unit
Returns a localized abbreviated form of the name of the duration unit.
For example in the EN locale, for 'qrt' it will return 'q'.
| Parameter | Type | Description |
|---|---|---|
unit | DurationUnit | Duration unit |
Localized abbreviated form of the name of the duration unit
Get the ratio between two units ( year, month -> 1/12 ).
| Parameter | Type | Description |
|---|---|---|
baseUnit | DurationUnit | Base time unit |
unit | DurationUnit | Time unit |
acceptEstimate | Boolean | If |
Ratio
Normalizes a unit for easier usage in conditionals. For example 'year', 'years', 'y' -> 'year'.
| Parameter | Type | Description |
|---|---|---|
unit | DurationUnit | Time unit |
Normalized unit name
Typedefs
2
Typedefs
2A string defining a duration unit. Valid values are:
- "millisecond" - Milliseconds
- "second" - Seconds
- "minute" - Minutes
- "hour" - Hours
- "day" - Days
- "week" - Weeks
- "month" - Months
- "quarter" - Quarters
- "year"- Years
A string defining an abbreviated duration unit. Valid values are:
- "ms" - Milliseconds
- "s" - Seconds
- "m" - Minutes
- "h" - Hours
- "d" - Days
- "w" - Weeks
- "M" - Months
- "q" - Quarters
- "y"- Years