Spanish Total.js
English
Documentation For AI agents
Contents

Date

Methods that jComponent adds to Date.prototype for formatting dates and calculating with them.

.md GitHub
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.
ParameterTypeDescription
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.

UnitAccepted names
Secondss, ss, sec, second, seconds
Minutesm, mm, min, minute, minutes
Hoursh, hh, hour, hours
Daysd, dd, day, days
Weeksw, ww, week, weeks
MonthsM, MM, month, months
Yearsy, yy, yyyy, year, years
Move a date
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.
ParameterTypeDescription
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.

TokenOutput for 18 November 2026, 14:05:09
yyyy, YYYY2026
yy, YY26
MMMMNovember
MMMNov
MM11
M11
dddd, DDDDWednesday
ddd, DDDWE
dd, DD18
d, D18
HH, hh14
H, h14
mm05
m5
ss09
s9
ww47 (ISO week, two digits)
w47 (ISO week)
aPM

Every token letter is replaced wherever it appears, and a format cannot contain an apostrophe, so write literal words outside the format.

Date formats
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"
UTC output
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"
Default format
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.

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.

Convert any value
var date = new Date(2026, 10, 18);
date.parseDate() === date; // true
method

date.toNumber([format])

Writes the date as a number.
ParameterTypeDescription
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.

Date as a number
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.
ParameterTypeDescription
ticks? Boolean Use true to get the milliseconds in place of a Date.

Returns Date or Number The shifted date, or its milliseconds.

Shift to UTC
var date = new Date(Date.UTC(2026, 10, 18, 13, 5));

date.toUTC().getHours(); // 13
date.toUTC(true) === date.getTime() + date.getTimezoneOffset() * 60000; // true
Made in Europe