Date
Methods that jComponent adds to Date.prototype for formatting dates and calculating with them.
On this page
jComponent adds these methods to Date.prototype. Month and day names come from the global arrays MONTHS and DAYS, which the Localization guide covers, and the default format is set in DEF. Text is converted to a date with parseDate().
API
method
date.add(type, [value])
Returns a new date moved by an amount of time.
date.add(type, [value])| Parameter | Type | Description |
|---|---|---|
type |
String |
The unit, or the whole amount as text. |
value? |
Number |
The amount of the unit. |
Returns Date A new date.
The original date is not changed. The amount is written as one text, such as '2 days', or as a unit and a number. A negative number moves the date back.
| Unit | Accepted names |
|---|---|
| Seconds | s, ss, sec, second, seconds |
| Minutes | m, mm, min, minute, minutes |
| Hours | h, hh, hour, hours |
| Days | d, dd, day, days |
| Weeks | w, ww, week, weeks |
| Months | M, MM, month, months |
| Years | y, yy, yyyy, year, years |
var date = new Date(2026, 10, 18, 14, 5, 9);
var f = 'yyyy-MM-dd HH:mm:ss';
date.add('1 day').format(f); // "2026-11-19 14:05:09"
date.add('-1 day').format(f); // "2026-11-17 14:05:09"
date.add('2 hours').format(f); // "2026-11-18 16:05:09"
date.add('min', 30).format(f); // "2026-11-18 14:35:09"
date.add('M', 2).format(f); // "2027-01-18 14:05:09"
date.add('2 weeks').format(f); // "2026-12-02 14:05:09"
date.format(f); // "2026-11-18 14:05:09"
method
date.format([format], [utc])
Writes the date as text.
date.format([format], [utc])| Parameter | Type | Description |
|---|---|---|
format? |
String |
The format of the output. Default: DEF.dateformat |
utc? |
Boolean |
Writes the date in UTC. |
Returns String The formatted date.
Without format, the method uses DEF.dateformat, which is yyyy-MM-dd by default. A format in the form [key] is read from ENV(). With utc, or when DEF.dateformatutc is true, the date is written in UTC. Use date.format('iso', true) for an ISO 8601 text.
| Token | Output for 18 November 2026, 14:05:09 |
|---|---|
yyyy, YYYY | 2026 |
yy, YY | 26 |
MMMM | November |
MMM | Nov |
MM | 11 |
M | 11 |
dddd, DDDD | Wednesday |
ddd, DDD | WE |
dd, DD | 18 |
d, D | 18 |
HH, hh | 14 |
H, h | 14 |
mm | 05 |
m | 5 |
ss | 09 |
s | 9 |
ww | 47 (ISO week, two digits) |
w | 47 (ISO week) |
a | PM |
Every token letter is replaced wherever it appears, and a format cannot contain an apostrophe, so write literal words outside the format.
var date = new Date(2026, 10, 18, 14, 5, 9);
date.format(); // "2026-11-18"
date.format('dd.MM.yyyy HH:mm:ss'); // "18.11.2026 14:05:09"
date.format('dddd, d MMMM yyyy'); // "Wednesday, 18 November 2026"
date.format('MMM yy'); // "Nov 26"
date.format('HH:mm a'); // "14:05 PM"
date.format('yyyy-ww'); // "2026-47"
var date = new Date(Date.UTC(2026, 10, 18, 13, 5, 9, 7));
date.format('HH:mm', true); // "13:05"
date.format('iso', true); // "2026-11-18T13:05:09.007Z"
var date = new Date(2026, 10, 18);
DEF.dateformat = 'dd/MM/yyyy';
date.format(); // "18/11/2026"
DEF.dateformat = 'yyyy-MM-dd';
ENV('short', 'dd.MM.');
date.format('[short]'); // "18.11."
method
date.parseDate()
Returns the same date.
date.parseDate()Returns Date The same date object.
Strings, numbers and dates all have a parseDate() method, so a value of any of these types can be converted without checking its type.
var date = new Date(2026, 10, 18);
date.parseDate() === date; // true
method
date.toNumber([format])
Writes the date as a number.
date.toNumber([format])| Parameter | Type | Description |
|---|---|---|
format? |
String |
A format() layout that contains only digits. Default: 'yyyyMMdd' |
Returns Number The date as a number.
The number is useful for comparing dates.
var date = new Date(2026, 10, 18, 14, 5);
date.toNumber(); // 20261118
date.toNumber('yyyyMM'); // 202611
date.toNumber('yyyyMMddHHmm'); // 202611181405
method
date.toUTC([ticks])
Shifts the date by the time zone offset, so its local fields show the UTC time.
date.toUTC([ticks])| Parameter | Type | Description |
|---|---|---|
ticks? |
Boolean |
Use true to get the milliseconds in place of a Date. |
Returns Date or Number The shifted date, or its milliseconds.
var date = new Date(Date.UTC(2026, 10, 18, 13, 5));
date.toUTC().getHours(); // 13
date.toUTC(true) === date.getTime() + date.getTimezoneOffset() * 60000; // true