KScript docs
EN

date fields and methods

Dates and times, from @now(), '2026-11-19'->to_date() or ->to_date().

A field is a plain fact about the value, read with a dot and no brackets: $value.name. A method takes arguments or does work, and is called with ->: $value->name(arguments), or with the value piped in: $value |> name(arguments).

Fields

FieldTypeDescription
.daynumberThe day of the month.
.day_of_yearnumberThe day of the year, 1 to 366.
.days_in_monthnumberHow many days the date's month has, 28 to 31.
.hournumberThe hour, 0 to 23.
.is_futureboolTrue when the date is after now. Tests can fix now with @with_clock().
.is_pastboolTrue when the date is before now. Tests can fix now with @with_clock().
.is_todayboolTrue when the date falls on today's date.
.is_weekendboolTrue when the date is a Saturday or a Sunday.
.minutenumberThe minute.
.monthnumberThe month, 1 to 12.
.quarternumberThe quarter of the year, 1 to 4.
.secondnumberThe second.
.weeknumberThe ISO 8601 week number, 1 to 53. Weeks start on Monday, and week 1 holds the year's first Thursday.
.weekdaystringThe day of the week, such as Monday.
.yearnumberThe year.

From KScript.Core

#date->add(years?: number, months?: number, weeks?: number, days?: number) → date

The date this many years, months, weeks and days later: ->add(months: 1, days: 2). Months and years keep the day of the month where they can (31 January plus a month is 28 or 29 February). For hours and minutes, add a duration: $date + 2h30m.
ParameterTakes
years optionalnumber
months optionalnumber
weeks optionalnumber
days optionalnumber

Arguments can be named: ->add(…, months: …, weeks: …).

Example
<echo>{ '2026-11-30'->to_date()->add(months: 3, days: 1)->format() }</echo>
echo '2026-11-30'->to_date()->add(months: 3, days: 1)->format()

#date->date() → date

The date at midnight.
Example
<echo>{ '2026-11-19 14:30'->to_date()->date()->format('yyyy-MM-dd HH:mm') }</echo>
echo '2026-11-19 14:30'->to_date()->date()->format('yyyy-MM-dd HH:mm')

#date->end_of(unit: day|week|month|quarter|year, week_start?: monday|tuesday|wednesday|thursday|friday|saturday|sunday) → date

The last moment of the day, week, month, quarter or year the date is in: ->end_of(month) is 23:59:59 on its last day.
ParameterTakes
unit requiredone of these words: day|week|month|quarter|year
week_start optionalone of these words: monday|tuesday|wednesday|thursday|friday|saturday|sunday

Arguments can be named: ->end_of(…, week_start: …).

Example
<echo>{ '2026-11-19 14:30'->to_date()->end_of(week, week_start: monday)->format('yyyy-MM-dd HH:mm') }</echo>
echo '2026-11-19 14:30'->to_date()->end_of(week, week_start: monday)->format('yyyy-MM-dd HH:mm')

#date->format(pattern?: date_format) → string

Writes the date the way a date_format says: ->format(dd-MM-yyyy), ->format('d MMMM yyyy') or ->format('HH:mm'). yyyy-MM-dd by default.
ParameterTakes
pattern optionaldate_format
Example
<echo>{ '2026-11-19 14:30'->to_date()->format('dddd d MMMM yyyy, HH:mm') }</echo>
echo '2026-11-19 14:30'->to_date()->format('dddd d MMMM yyyy, HH:mm')

#date->is_same(other: date, unit?: day|week|month|quarter|year) → bool

True when both dates are in the same day (the default), week, month, quarter or year.
ParameterTakes
other requireddate
unit optionalone of these words: day|week|month|quarter|year

Arguments can be named: ->is_same(…, unit: …).

Example
<echo>{ '2026-11-19 09:00'->to_date()->is_same('2026-11-30'->to_date(), month) }</echo>
echo '2026-11-19 09:00'->to_date()->is_same('2026-11-30'->to_date(), month)

#date->iso() → string

The date in ISO 8601 format.
Example
<echo>{ '2026-11-19 14:30'->to_date()->iso() }</echo>
echo '2026-11-19 14:30'->to_date()->iso()

#date->since(other: date, in: days|weeks|months|years) → number

The whole days, weeks, months or years from other to this date, negative when other is later. Months and years count the way a birthday does: @today()->since($born, in: years) is an age. For the exact time between two dates, subtract them: $finish - $start is a duration.
ParameterTakes
other requireddate
in requiredone of these words: days|weeks|months|years

Arguments can be named: ->since(…, in: …).

Example
<echo>{ '2026-11-19'->to_date()->since('1990-12-01'->to_date(), in: years) }</echo>
echo '2026-11-19'->to_date()->since('1990-12-01'->to_date(), in: years)

#date->start_of(unit: day|week|month|quarter|year, week_start?: monday|tuesday|wednesday|thursday|friday|saturday|sunday) → date

The first moment of the day, week, month, quarter or year the date is in. A week starts on the locale's first day unless week_start: says otherwise.
ParameterTakes
unit requiredone of these words: day|week|month|quarter|year
week_start optionalone of these words: monday|tuesday|wednesday|thursday|friday|saturday|sunday

Arguments can be named: ->start_of(…, week_start: …).

Example
<echo>{ '2026-11-19 14:30'->to_date()->start_of(month)->format('yyyy-MM-dd HH:mm') }</echo>
echo '2026-11-19 14:30'->to_date()->start_of(month)->format('yyyy-MM-dd HH:mm')

#date->subtract(years?: number, months?: number, weeks?: number, days?: number) → date

The date this many years, months, weeks and days earlier: ->subtract(days: 7). For hours and minutes, take away a duration: $date - 30m.
ParameterTakes
years optionalnumber
months optionalnumber
weeks optionalnumber
days optionalnumber

Arguments can be named: ->subtract(…, months: …, weeks: …).

Example
<echo>{ '2026-11-19'->to_date()->subtract(weeks: 2)->format() }</echo>
echo '2026-11-19'->to_date()->subtract(weeks: 2)->format()

#date->timestamp() → number

Seconds since 1970-01-01 UTC.
Example
<echo>{ '2026-01-01'->to_date()->timestamp() > 0 }</echo>
echo '2026-01-01'->to_date()->timestamp() > 0

Methods from any