v7.3.0
SupportExamplesFree Trial

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');

Note that date strings without timezone information ('Z' or '+-HH:mm') will be in the local timezone. Eg. '2024-05-14' -> local time, '2024-05-14Z' -> UTC

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');

Useful functions

Function Description
format Format a date to a string using tokens
parse Parse a string into a Date object
add Add a specified amount of time to a date
diff Get the difference between two dates
as Convert between time units

See also

No results

Properties

Properties are getters/setters or publicly accessible variables on this class
  • amIndicator : String
    READONLY
    static

    Get 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').

  • pmIndicator : String
    READONLY
    static

    Get the local PM indicator string.

  • weekStartDay : Number
    READONLY
    static

    Get the first day of week, 0-6 (Sunday-Saturday). This is determined by the current locale's DateHelper.weekStartDay parameter.

    Functions

    Functions are methods available for calling on the class
    • snap( )
      internal
      static

      Implementation for round, floor and ceil.

      Type definitions

      Source path

      Core/helper/DateHelper.js

      Contents