Date
Methods that jComponent adds to Date.prototype for date arithmetic, formatting and UTC conversion.
On this page
jComponent adds these methods to every date. The default format is set in DEF, text is converted to a date with parseDate(), and the month and day names come from the global arrays MONTHS and DAYS.
API
method
Date.add(value)
Returns a new date moved by a duration.
Date.add(value)| Parameter | Type | Description |
|---|---|---|
value |
String |
The duration. |
Returns Date A new date. The original date does not change.
The value is a number, a space and a unit. A negative number moves the date back, and the number can be a [key] phrase set with ENV(). An unknown unit returns an unchanged copy.
| Unit | Accepted names |
|---|---|
| Seconds | seconds, second, sec, ss, s |
| Minutes | minutes, minute, min, mm, m |
| Hours | hours, hour, hh, h |
| Days | days, day, dd, d |
| Weeks | weeks, week, ww, w |
| Months | months, month, MM, M |
| Years | years, year, yyyy, yy, y |
var date = new Date(2026, 0, 15, 10, 0, 0);
console.log(date.add('1 day').format('yyyy-MM-dd')); // 2026-01-16
console.log(date.add('-1 month').format('yyyy-MM-dd')); // 2025-12-15
console.log(date.add('2 hours').format('HH:mm')); // 12:00
console.log(date.add('30 m').format('HH:mm')); // 10:30
console.log(date.format('yyyy-MM-dd HH:mm')); // 2026-01-15 10:00
method
Date.format([format], [utc])
Writes the date as text.
Date.format([format], [utc])| Parameter | Type | Description |
|---|---|---|
format? |
String |
The layout of the text. Default: DEF.dateformat |
utc? |
Boolean |
true writes the date in UTC. Default: false |
Returns String The formatted date.
Every token in format is replaced, so other letters that match a token cannot be used as literal text.
| Token | Output | Example |
|---|---|---|
yyyy, YYYY | Full year | 2026 |
yy, YY | Two-digit year | 26 |
MMMM | Month name | November |
MMM | First three letters of the month | Nov |
MM | Month with two digits | 11 |
M | Month | 11 |
dddd, DDDD | Day name | Sunday |
ddd, DDD | First two letters of the day in uppercase | SU |
dd, DD | Day with two digits | 08 |
d, D | Day | 8 |
HH, hh | Hours with two digits (0 to 23) | 15 |
H, h | Hours (0 to 23) | 15 |
mm | Minutes with two digits | 04 |
m | Minutes | 4 |
ss | Seconds with two digits | 05 |
s | Seconds | 5 |
ww | ISO week with two digits | 45 |
w | ISO week | 45 |
a | AM or PM | PM |
Start the format with ! to write the hours on a 12-hour clock, from 1 to 12, for example !hh:mm a. A [key] format uses the format set with ENV().
format('iso') returns the ISO 8601 date in UTC, the same as toISOString(), and an empty string for an invalid date. Setting DEF.dateformatutc to true makes every call use UTC.
var date = new Date(2026, 10, 8, 15, 4, 5);
console.log(date.format()); // 2026-11-08
console.log(date.format('dd.MM.yyyy HH:mm')); // 08.11.2026 15:04
console.log(date.format('dddd, d MMMM yyyy')); // Sunday, 8 November 2026
console.log(date.format('MMM yy')); // Nov 26
console.log(date.format('ww')); // 45
var date = new Date(Date.UTC(2026, 10, 8, 15, 4, 5));
console.log(date.format('yyyy-MM-dd HH:mm', true)); // 2026-11-08 15:04
console.log(date.format('iso')); // 2026-11-08T15:04:05.000Z
var date = new Date(2026, 10, 8, 12, 30);
console.log(date.format('!hh:mm a')); // 12:30 PM
ENV('short', 'dd.MM.');
console.log(date.format('[short]')); // 08.11.
ENV('short', 'MM/dd');
console.log(date.format('[short]')); // 11/08
method
Date.toUTC([ticks])
Moves the date by the time zone offset, so that its local fields show the UTC time.
Date.toUTC([ticks])| Parameter | Type | Description |
|---|---|---|
ticks? |
Boolean |
true returns the timestamp in milliseconds in place of a Date. Default: false |
Returns Date or Number The converted date, or its timestamp.
var date = new Date(Date.UTC(2026, 10, 8, 15, 4));
console.log(date.toUTC().format('yyyy-MM-dd HH:mm')); // 2026-11-08 15:04
console.log(date.toUTC(true) === date.toUTC().getTime()); // true