Skip to content

yugajs

Imported from yugajs.

new Yuga(config): Yuga;
Parameter Type
config yuga.Config

Yuga

yuga.Yuga.constructor

Property Modifier Type Inherited from
d readonly number yuga.Yuga.d
D readonly number yuga.Yuga.D
date readonly { (): number; (value): yuga.Yuga; } yuga.Yuga.date
day readonly { (): number; (value): yuga.Yuga; } yuga.Yuga.day
h readonly number yuga.Yuga.h
hour readonly { (): number; (value): yuga.Yuga; } yuga.Yuga.hour
m readonly number yuga.Yuga.m
M readonly number yuga.Yuga.M
millisecond readonly { (): number; (value): yuga.Yuga; } yuga.Yuga.millisecond
minute readonly { (): number; (value): yuga.Yuga; } yuga.Yuga.minute
month readonly { (): number; (value): yuga.Yuga; } yuga.Yuga.month
ms readonly number yuga.Yuga.ms
quarter readonly { (): number; (value): yuga.Yuga; } yuga.Yuga.quarter
s readonly number yuga.Yuga.s
second readonly { (): number; (value): yuga.Yuga; } yuga.Yuga.second
week readonly { (): number; (value): yuga.Yuga; } yuga.Yuga.week
y readonly number yuga.Yuga.y
year readonly { (): number; (value): yuga.Yuga; } yuga.Yuga.year
get q(): number;

The quarter and week are computed on access since they are rarely used.

number

yuga.Yuga.q

get w(): number;

The ISO week of the year (weeks start on Monday, the first week contains January 4th).

number

yuga.Yuga.w

add(value, unit?): yuga.Yuga;

Adds an amount of a unit (milliseconds by default), Day.js’s add.

  • y, Q and M move the wall clock and clamp the date to the end of the month (Jan 31 + 1 month is Feb 29 in 2024), fractions are truncated.
  • w and d (or D) move the wall clock by whole days (fractions are rounded), keeping the time of day across DST.
  • h, m, s and ms are absolute time (an hour across DST is 60 minutes later).

Units are case-insensitive. Lowercase q is a quarter (Day.js adds milliseconds).

Parameter Type
value number
unit? yuga.Unit

yuga.Yuga

yuga("2024-01-31").add(1, "month"); // 2024-02-29

yuga.Yuga.add

calendar(referenceTime?, formats?): string;

The instance in calendar words relative to a reference time ("Today at 10:20 AM", "Last Monday at 9:00 AM", "05/15/2024"), Day.js’s calendar plugin. The bucket is the day difference from the start of the reference’s day, in the instance’s time zone.

Parameter Type Description
referenceTime? yuga.Parsable What to compare with, now by default.
formats? CalendarFormats The format of each bucket, a template or a function; they win over the locale’s.

string

yuga.Yuga.calendar

clone(): yuga.Yuga;

Creates a clone of the current Yuga class.

Yuga doesn’t support mutability, this function is ported from Moment.js & Day.js. While Day.js does default to immutability, plugins such as badMutable allow it, Yuga does not provide such plugins.

yuga.Yuga

yuga.Yuga.clone

dates(): number;

An alias of date(), the day of the month.

number

yuga.Yuga.dates

dates(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

yuga.Yuga.dates

dayOfYear(): number;

The day of the year in the instance’s time zone, 1 to 366 (Day.js’s dayOfYear plugin). The setter moves to that day of the year, the same as add(n - dayOfYear(), "day").

number

yuga.Yuga.dayOfYear

dayOfYear(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

yuga.Yuga.dayOfYear

days(): number;

An alias of day(), the day of the week.

number

yuga.Yuga.days

days(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

yuga.Yuga.days

daysInMonth(): number;

Get the number of days in the current month.

NaN when invalid.

number

yuga.Yuga.daysInMonth

diff(
that?,
unit?,
options?
): number;

The difference from a parsable (now when omitted) to the instance in a unit, milliseconds by default. NaN when either is invalid (for every unit, Day.js gives 0 for months).

By default it matches Day.js: months, quarters and years use Moment.js’s month difference (2024-05-15 to 2024-01-31 is 3.48 months), days and weeks discount the change of the UTC offset, and the result is truncated toward zero.

The parsable is compared in the instance’s time zone, the same as Moment.js.

Parameter Type Description
that? yuga.Parsable -
unit? yuga.Unit -
options? boolean | yuga.DiffOptions true keeps the fraction (Day.js’s float), see DiffOptions.

number

yuga("2024-05-15").diff("2024-01-31", "month", true); // 3.48…

yuga.Yuga.diff

endOf(unit): yuga.Yuga;

The end of a unit, the start of the next one minus 1 ms (endOf("day") is 23:59:59.999).

Parameter Type
unit yuga.Unit | "isoWeek"

yuga.Yuga

startOf

yuga.Yuga.endOf

format(template?): string;

Formats with a template, Day.js’s format and its advancedFormat tokens, in the instance’s locale. Text in brackets is escaped ("[Today is] dddd").

  • YY YYYY M MM MMM MMMM D DD Do d dd ddd dddd H HH h hh a A m mm s ss SSS Z ZZ Q
  • w ww the week of the locale (its weekStart and yearStart), W WW the ISO week.

With no template it is ISO 8601 with the offset ("2024-05-15T06:00:00-04:00", and "…Z" in UTC), invalid instances are the locale’s invalidDate ("Invalid Date").

Parameter Type
template? string

string

yuga("2024-05-15").format("dddd, MMMM Do YYYY"); // "Wednesday, May 15th 2024"

yuga.Yuga.format

from(date?, withoutSuffix?): string;

The time from a date to the instance in words, yuga("2024-01-01").from("2024-01-03") is "2 days ago".

Parameter Type
date? yuga.Parsable
withoutSuffix? boolean

string

yuga.Yuga.from

fromNow(withoutSuffix?): string;

The time from now to the instance in words ("2 hours ago", "in a day"), Day.js’s relativeTime plugin. Past is “ago”, future is “in”.

Parameter Type Description
withoutSuffix? boolean Drop the “ago” / “in” ("2 hours").

string

yuga.Yuga.fromNow

get(): string;

Get the string value of the class.

Equivalent to yuga().toString()

string

yuga.Yuga.get

get(unit): number;

Get the numeric value of the provided unit.

Equivalent to yuga()unit.

Parameter Type Description
unit yuga.Unit The unit of the desired value to get.

number

yuga.Yuga.get

hours(): number;

An alias of hour().

number

yuga.Yuga.hours

hours(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

yuga.Yuga.hours

isAfter(that, unit?): boolean;

Checks if the instance is after a parsable, with a unit: if the parsable is before the start of the instance’s unit.

The unit is in the instance’s time zone and locale (see startOf).

Parameter Type
that yuga.Parsable
unit? yuga.Unit | "isoWeek"

boolean

yuga.Yuga.isAfter

isBefore(that, unit?): boolean;

Checks if the instance is before a parsable, with a unit: if the end of the instance’s unit is before it (isBefore(x, "day") is true when x is on a later day).

The unit is in the instance’s time zone and locale (see startOf).

Parameter Type
that yuga.Parsable
unit? yuga.Unit | "isoWeek"

boolean

yuga.Yuga.isBefore

isBetween(
a,
b,
unit?,
inclusivity?
): boolean;

Checks if the instance is between two parsables, given in either order. Exclusive by default ("()"), "[" and "]" include the lower and upper bound ("[]", "[)", "(]"). With a unit the comparison is by unit (see isBefore and isAfter).

Parameter Type
a yuga.Parsable
b yuga.Parsable
unit? yuga.Unit | "isoWeek"
inclusivity? "()" | "[]" | "[)" | "(]"

boolean

yuga.Yuga.isBetween

isLeapYear(): boolean;

Checks if the instance’s year (in its time zone) is a leap year (Day.js’s isLeapYear plugin). false when invalid.

boolean

yuga.Yuga.isLeapYear

isoWeek(): number;

The ISO week of the year, an alias of week() for Day.js code.

number

yuga.Yuga.isoWeek

isoWeek(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

yuga.Yuga.isoWeek

isoWeekday(): number;

The ISO day of the week, Monday is 1 and Sunday is 7. The setter moves within the Monday to Sunday week.

number

yuga.Yuga.isoWeekday

isoWeekday(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

yuga.Yuga.isoWeekday

isoWeeks(): number;

An alias of isoWeek(), the ISO week of the year.

number

yuga.Yuga.isoWeeks

isoWeeks(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

yuga.Yuga.isoWeeks

isoWeeksInYear(): 52 | 53;

The number of ISO weeks in the year, 52 or 53.

52 | 53

yuga.Yuga.isoWeeksInYear

isoWeekYear(): number;

The year the ISO week belongs to (December 30th 2024 is in 2025).

number

yuga.Yuga.isoWeekYear

isSame(that, unit?): boolean;

Checks if the instance is the same moment as a parsable, with a unit: if the parsable is within the instance’s unit (isSame(x, "week") is the same Sunday to Saturday week in en).

The unit is in the instance’s time zone and locale (see startOf).

Parameter Type
that yuga.Parsable
unit? yuga.Unit | "isoWeek"

boolean

yuga.Yuga.isSame

isSameOrAfter(that, unit?): boolean;

Checks if the instance is the same as or after a parsable (by unit, see isAfter).

Parameter Type
that yuga.Parsable
unit? yuga.Unit | "isoWeek"

boolean

yuga.Yuga.isSameOrAfter

isSameOrBefore(that, unit?): boolean;

Checks if the instance is the same as or before a parsable (by unit, see isBefore).

Parameter Type
that yuga.Parsable
unit? yuga.Unit | "isoWeek"

boolean

yuga.Yuga.isSameOrBefore

isToday(): boolean;

Checks if the instance is today, in its time zone (Day.js’s isToday plugin).

Day.js compares with the system’s time zone, yugajs with the instance’s, the same as isSame(…, "day"). It’s false when invalid.

boolean

yuga.Yuga.isToday

isTomorrow(): boolean;

Checks if the instance is tomorrow, in its time zone (Day.js’s isTomorrow plugin).

boolean

yuga.Yuga.isTomorrow

isUTC(): boolean;

Check if the instance is in UTC (the "UTC" time zone, and its names "Etc/UTC", "Etc/GMT" and "+00:00").

boolean

yuga.Yuga.isUTC

isValid(): boolean;

Get the validity of the class, opposite of “Invalid Date” in the JS Date object.

Equivalent to !isNaN(yuga().$e).

boolean

yuga.Yuga.isValid

isYesterday(): boolean;

Checks if the instance is yesterday, in its time zone (Day.js’s isYesterday plugin).

boolean

yuga.Yuga.isYesterday

local(keepLocalTime?): yuga.Yuga;

The same moment in the system’s time zone.

Parameter Type Description
keepLocalTime? boolean Keep the wall clock rather than the moment. A yugajs addition, mirroring utc(true) and tz(zone, true).

yuga.Yuga

yuga.Yuga.local

locale(): string;

Get the name of the instance’s locale ("en").

string

yuga.Yuga.locale

locale(preset, object?): yuga.Yuga;

A clone in another locale, Day.js’s locale. Names are any BCP 47 tag (built out of Intl) or registered locales, an object registers it under the name. An unknown locale keeps the current one.

The global locale stays as is, see yuga.locale.

Parameter Type
preset string | yuga.Locale
object? yuga.Locale

yuga.Yuga

yuga("2024-05-15").locale("fr").format("dddd D MMMM"); // "mercredi 15 mai"

yuga.Yuga.locale

localeData(): yuga.LocaleData;

The data of the instance’s locale: names, first day of the week, formats, meridiem and ordinal. months(instance) and its siblings return the name for that instance.

Available with yugajs/extension/locale-data.

yuga.LocaleData

yuga.Yuga.localeData

milliseconds(): number;

An alias of millisecond().

number

yuga.Yuga.milliseconds

milliseconds(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

yuga.Yuga.milliseconds

minutes(): number;

An alias of minute().

number

yuga.Yuga.minutes

minutes(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

yuga.Yuga.minutes

months(): number;

An alias of month().

number

yuga.Yuga.months

months(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

yuga.Yuga.months

offsetName(type?): string;

The name of the instance’s time zone at its moment, always in en-US like Day.js: "EDT" ("short", the default) or "Eastern Daylight Time" ("long").

Throws a RangeError when invalid, the same as Day.js. UTC instances are "UTC" (Day.js gives the system’s name).

Parameter Type
type? "short" | "long"

string

yuga.Yuga.offsetName

quarters(): number;

An alias of quarter().

number

yuga.Yuga.quarters

quarters(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

yuga.Yuga.quarters

seconds(): number;

An alias of second().

number

yuga.Yuga.seconds

seconds(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

yuga.Yuga.seconds

set(uglyUnit, value?): yuga.Yuga;

Set the numeric value of the provided unit.

Equivalent to yuga()[unit](value).

In the repeated hour of a DST fall back the instance’s offset is kept when it’s valid for the new wall clock (set("minute", 0) of the second 01:30 is the second 01:00), the same as Temporal’s with and Luxon. Other wall clocks the time zone repeats are the earlier offset, and ones it skips move forward.

Parameter Type Description
uglyUnit yuga.Unit The unit of the desired value to set.
value? number The new value.

yuga.Yuga

yuga.Yuga.set

startOf(unit): yuga.Yuga;

The start of a unit in the instance’s time zone (startOf("month") is the 1st at 00:00).

  • "week" starts on the weekStart of the instance’s locale (Sunday for en, Monday for en-gb), "isoWeek" always starts on Monday.
  • Calendar units are wall-clock midnights, a midnight the time zone skips moves forward.
  • h, m and s round the wall clock down, Temporal’s round with "floor": they keep the instance’s offset when it’s valid (the repeated hour of a DST fall back starts at its own 01:00, the same as Moment.js), and start at the top of the wall-clock hour across a 30-minute offset change (Lord Howe).

Milliseconds, unknown and missing units return the same moment.

Parameter Type
unit yuga.Unit | "isoWeek"

yuga.Yuga

yuga.Yuga.startOf

subtract(value, unit?): yuga.Yuga;

Subtracts an amount of a unit, add with the value negated.

Parameter Type
value number
unit? yuga.Unit

yuga.Yuga

yuga("2024-03-31").subtract(1, "M"); // 2024-02-29

yuga.Yuga.subtract

to(date?, withoutSuffix?): string;

The time from the instance to a date in words, yuga("2024-01-01").to("2024-01-03") is "in 2 days".

Parameter Type
date? yuga.Parsable
withoutSuffix? boolean

string

yuga.Yuga.to

toArray(): [number, number, number, number, number, number, number];

[year, month, date, hour, minute, second, millisecond] in the instance’s time zone, the month is 0-based (Day.js’s toArray plugin).

[number, number, number, number, number, number, number]

yuga.Yuga.toArray

toDate(): Date;

Formats to a Date class equivalent value of the class.

Date

yuga.Yuga.toDate

toInstant(): Instant;

The Temporal.Instant of the instance, throws a RangeError when invalid.

Instant

yuga.Yuga.toInstant

toISOString(keepOffset?): string;

Formats to an ISO 8601 string in UTC with milliseconds ("2024-05-15T10:00:00.000Z"), or at the instance’s own offset with keepOffset ("2024-05-15T12:00:00.000+02:00", Moment.js’s toISOString(true)).

Equivalent to yuga().toDate().toISOString(), throws a RangeError when invalid. For the time zone too, RFC 9557’s "2024-05-15T12:00:00+02:00[Europe/Paris]", use toTemporal().toString(): yuga() parses it back to the same instance.

Parameter Type
keepOffset? boolean

string

yuga.Yuga.toISOString

toJSON(): string | null;

Serializes to the ISO 8601 string, or null when invalid (used by JSON.stringify).

string | null

yuga.Yuga.toJSON

toNow(withoutSuffix?): string;

The time from the instance to now in words ("in 2 hours" for an instance in the past).

Parameter Type
withoutSuffix? boolean

string

yuga.Yuga.toNow

toObject(): object;

The date and time fields in the instance’s time zone, the month is 0-based (Day.js’s toObject plugin).

object

Name Type
date number
hours number
milliseconds number
minutes number
months number
seconds number
years number

yuga.Yuga.toObject

toPlainDate(): PlainDate;

The Temporal.PlainDate of the instance’s date in its time zone, throws a RangeError when invalid.

PlainDate

yuga.Yuga.toPlainDate

toString(): string;

Formats to a string in the instance’s time zone ("Wed May 15 2024 06:00:00 GMT-0400 (Eastern Daylight Time)"), “Invalid Date” when invalid.

Equivalent to yuga().toDate().toString() in the system’s time zone. Day.js returns the UTC string instead, see toUTCString.

string

yuga.Yuga.toString

toTemporal(): ZonedDateTime;

The Temporal.ZonedDateTime of the instance (its time zone, the system’s when it has none).

The same object as $T, built once. Throws a RangeError when invalid.

ZonedDateTime

yuga.Yuga.toTemporal

toUTCString(): string;

Formats to a UTC string ("Wed, 15 May 2024 10:00:00 GMT"), “Invalid Date” when invalid.

Equivalent to yuga().toDate().toUTCString() and Day.js’s toString().

string

yuga.Yuga.toUTCString

tz(): string;

Get the IANA time zone of the instance (the system’s when it has none of its own).

string

yuga.Yuga.tz

tz(zone, keepLocalTime?): yuga.Yuga;

The same moment in another time zone (an IANA name, or an offset such as "+05:30").

Parameter Type Description
zone string -
keepLocalTime? boolean Keep the wall clock rather than the moment (10:00 in New York becomes 10:00 in Tokyo).

yuga.Yuga

yuga("2024-05-15T10:00:00Z").tz("Asia/Tokyo").hour(); // 19

When the time zone is unknown.

yuga.Yuga.tz

unix(): number;

Get the number of seconds since the Unix Epoch, floored.

Equivalent to Math.floor(yuga().valueOf() / 1000).

number

yuga.Yuga.unix

utc(keepLocalTime?): yuga.Yuga;

The same moment in UTC, tz("UTC").

Parameter Type
keepLocalTime? boolean

yuga.Yuga

yuga.Yuga.utc

utcOffset(): number;

Get the offset from UTC in minutes, rounded to 15 minutes like Day.js (-240 for New York in summer).

NaN when invalid.

number

yuga.Yuga.utcOffset

utcOffset(offset, keepLocalTime?): yuga.Yuga;

The same moment in a fixed offset from UTC, DST doesn’t apply to it.

Parameter Type Description
offset string | number Minutes (-240), hours when 16 or less in absolute value (-4), or a string ("-04:00", "+0530", an unparsable one returns the same instance). 0 is utc().
keepLocalTime? boolean Keep the wall clock rather than the moment.

yuga.Yuga

yuga("2024-05-15T10:00:00Z").utcOffset(-240).format(); // "2024-05-15T06:00:00-04:00"

yuga.Yuga.utcOffset

valueOf(): number;

Get the number of milliseconds since the Unix Epoch of the Yuga class.

Equivalent to yuga().toDate().valueOf(), NaN when invalid.

number

yuga.Yuga.valueOf

weekday(): number;

The day of the week by the locale’s weekStart, 0 is the first day of the locale’s week (Sunday in en, Monday in en-gb). The setter moves within the week the same as day(n).

number

yuga.Yuga.weekday

weekday(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

yuga.Yuga.weekday

weeks(): number;

An alias of week().

number

yuga.Yuga.weeks

weeks(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

yuga.Yuga.weeks

weekYear(): number;

The year the week belongs to by the locale’s weekStart and yearStart.

number

yuga.Yuga.weekYear

years(): number;

An alias of year().

number

yuga.Yuga.years

years(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

yuga.Yuga.years

static create(
epoch,
zone,
localeName?,
temporal?
): yuga.Yuga;

Creates an instance straight from epoch milliseconds (NaN for invalid) in a time zone (undefined for the system’s), skipping parsing. This is how every method that returns a new instance creates it.

Parameter Type Description
epoch number -
zone string | undefined -
localeName? string The name of a registered locale, the global locale by default.
temporal? ZonedDateTime | null The zoned date time of the same epoch and time zone if one was already built, it becomes the $T cache.

yuga.Yuga

readonly $e: number;

Milliseconds since the Unix Epoch, NaN when invalid. Along with $z it is the whole state of the instance, the unit fields below are derived from both by yuga.$.cascade.

yuga.Yuga.$e

readonly $isYugaClass: true;

yuga.Yuga.$isYugaClass

readonly $l: string;

The name of the instance’s locale ("en"), the global locale when it was created.

yuga.Yuga.$l

readonly $z: string | undefined;

The IANA time zone of the instance, undefined is the system’s (which Date follows).

yuga.Yuga.$z

get $T(): ZonedDateTime | null;

The zoned date time of the instance, null when invalid.

Built on first access and cached, the hot paths (parsing, getters, comparisons and most setters in the system’s time zone or UTC) don’t need it.

ZonedDateTime | null

yuga.Yuga.$T

get $v(): boolean;

boolean

yuga.Yuga.$v

Property Type Description
format? string | readonly string[] The format of a string parsable ("DD/MM/YYYY HH:mm"), or a list of formats where the first that parses wins. Needs yugajs/extension/custom-parse-format, strings with a format are invalid without it (see yuga.$.parseFormatted).
locale? string | yuga.Locale The locale of the instance, a name or an object to register (see yuga.locale). The global locale by default, or the locale of a parsed instance.
parsable? yuga.Parsable -
strict? boolean Strict format parsing, a string that doesn’t format back to itself is invalid (see format).
utc? boolean Use the "UTC" time zone instead of the system’s, parsables that have a time zone of their own (Yuga instances, Temporal.ZonedDateTime) keep it.
zone? string The time zone of the instance (an IANA name, or an offset such as "+05:30"), it takes precedence over utc and the time zone of the parsable. Wall-clock values are read in it.

The options of diff, a boolean is the same as { float } (Day.js’s third argument).

Property Type Description
exact? boolean Use Temporal’s calendar arithmetic (ZonedDateTime#until totaled in the unit) instead of Day.js’s algorithm: Jan 31 to Feb 29 is exactly 1 month.
float? boolean Keep the fraction instead of truncating toward zero.

An amount of time, immutable. Lengths are fixed (a week is 7 days, a year 365 days and a month 365 / 12 days), the same as Day.js.

add(value, unit?): yuga.Duration;

A new duration with a value added (a number of the unit, an object, a string or a duration), balanced like a number.

Parameter Type
value yuga.DurationInput
unit? yuga.Unit

yuga.Duration

as(unit): number;

The whole duration in a unit (duration(90, "minutes").as("hours") is 1.5).

Parameter Type
unit yuga.Unit

number

asDays(): number;

number

asHours(): number;

number

asMilliseconds(): number;

number

asMinutes(): number;

number

asMonths(): number;

number

asSeconds(): number;

number

asWeeks(): number;

number

asYears(): number;

number

clone(): yuga.Duration;

yuga.Duration

days(): number;

number

format(template?): string;

The duration with tokens Y YY YYYY M MM D DD H HH m mm s ss SSS and [escaped text], "YYYY-MM-DDTHH:mm:ss" by default.

A part the duration doesn’t have is 0 (Day.js prints “undefined”).

Parameter Type
template? string

string

get(unit): number;

A part of the duration: milliseconds is the total modulo 1000, weeks the total divided by 7 days (truncated), others are the part (balanced for a number, as given for an object).

Parameter Type
unit yuga.Unit

number

hours(): number;

number

humanize(withSuffix?): string;

The duration in words ("2 hours", "in 2 hours"), Day.js’s humanize with the thresholds of relativeTime.

Months and years use the fixed lengths, Day.js measures them from today.

Parameter Type Description
withSuffix? boolean Add the “in” / “ago” ("in 2 hours", "2 hours ago" when negative).

string

locale(): string;

The locale name of the duration (humanize uses it), the global locale when it was created.

string

locale(preset): yuga.Duration;
Parameter Type
preset string | yuga.Locale

yuga.Duration

milliseconds(): number;

number

minutes(): number;

number

months(): number;

number

seconds(): number;

number

subtract(value, unit?): yuga.Duration;
Parameter Type
value yuga.DurationInput
unit? yuga.Unit

yuga.Duration

toISOString(): string;

ISO 8601 ("P1DT2H"), weeks become days, "P0D" when empty.

string

toJSON(): string;

string

toTemporal(): Duration;

A Temporal.Duration with the same parts. A number’s balanced parts are all of the same sign.

Temporal needs whole years, months, weeks and days (a RangeError otherwise, as it is for parts of different signs, an invalid duration is empty); fractions of hours, minutes and seconds go to the smaller parts.

Duration

valueOf(): number;

The milliseconds, so durations compare and subtract.

number

weeks(): number;

number

years(): number;

number


Every value yuga() can parse, see yuga.$.parse for how each is read.

Property Type Description
array readonly number[] The wall-clock values [year, month, date, hour, minute, second, millisecond], the same order as the Date constructor’s arguments (the month is 0-indexed), read in the instance’s time zone. Missing values are the start ([2024, 4] is May 1st), [] is now. Available with yugajs/extension/array-support.
bigInt bigint Epoch milliseconds as a BigInt (yuga(1715767200000n)), out of range is invalid. Available with yugajs/extension/big-int-support.
dateClass Date -
dateString string Can be used to parse custom strings.
instant Instant -
itself yuga.Yuga -
null null Returns yuga representation of an Invalid Date equivalent to yuga(new Date(NaN)). Backport from moment, moment sees it as parsing an invalid moment object. TODO: Should this even be allowed? should it be Invalid?
object yuga.UnitValues A plain object of values by unit ({ year: 2024, month: 4, date: 15, hour: 10 }), read in the instance’s time zone. Missing parts are the start, except that the ones above the greatest unit given are the current ones ({ hour: 10 } is today at 10:00). {} is now. Available with yugajs/extension/object-support.
plainDate PlainDate A wall-clock date at midnight, read in the instance’s time zone. Available with yugajs/extension/plain-date-support.
plainDateTime PlainDateTime A wall-clock date and time, read in the instance’s time zone. Available with yugajs/extension/plain-date-support.
timestamp number -
undefined undefined Returns yuga representation of now equivalent to yuga(new Date()).
zonedDateTime ZonedDateTime -

Factory(parsable?): yuga.Yuga;

Create an instance, now by default (see Parsable). Strings without a format are ISO 8601 and Day.js’s loose YYYY-MM-DD HH:mm:ss.SSS in the system’s time zone.

Parameter Type
parsable? yuga.Parsable

yuga.Yuga

yuga("2024-05-15 10:20");
Factory(
parsable,
format,
locale?,
strict?
): yuga.Yuga;

Create an instance from a string in a format (Day.js’s customParseFormat), and optionally in a locale (for the names of months and weekdays, and the instance’s locale).

Needs yugajs/extension/custom-parse-format (yuga.extend(customParseFormat)), strings with a format are invalid without it.

Tokens: Y YYYY YY M MM MMM MMMM D DD Do d dd ddd dddd w ww H HH h hh m mm s ss S SS SSS A a Q Z ZZ X x, text in brackets is literal. Missing fields default the same as Day.js (the current year, the 1st of the month, midnight), strings that don’t match are invalid, and so are unknown weekday and month names.

A list of formats tries each in order and keeps the first valid result. Strict parsing (true as the last argument) makes strings that don’t format back to themselves invalid: overflowing values ("2024-02-30") and weekdays that don’t match the date.

Parameter Type
parsable yuga.Parsable
format string | readonly string[]
locale? string | yuga.Locale
strict? boolean

yuga.Yuga

yuga("15/05/2024", "DD/MM/YYYY");
yuga("15 mai 2024", "D MMMM YYYY", "fr");
yuga("2024-02-30", "YYYY-MM-DD", true).isValid(); // false
yuga("2024-05-15", ["DD/MM/YYYY", "YYYY-MM-DD"]);
Factory(
parsable,
format,
strict
): yuga.Yuga;
Parameter Type
parsable yuga.Parsable
format string | readonly string[]
strict boolean

yuga.Yuga

Factory(parsable, options): yuga.Yuga;

Create an instance with options, Day.js’s dayjs(date, { utc, locale }).

Parameter Type
parsable yuga.Parsable
options yuga.Options

yuga.Yuga

yuga("2024-05-15 10:20", { utc: true });
yuga("2024-05-15", { locale: "es" });
Property Type Description
en yuga.Locale The built-in English locale (Day.js’s en).
locales Record<string, yuga.Locale> Every registered locale, keyed by name (built-in en, and every locale resolved so far).
Ls Record<string, yuga.Locale> Day.js’s name of locales.
tz { (parsable, zone?): yuga.Yuga; (parsable, format, zone): yuga.Yuga; guess(): string; setDefault(zone?): void; } Create an instance in a time zone (Day.js’s dayjs.tz): wall-clock values (strings without an offset, plain Temporal values) are read in it, moments (numbers, dates, instants, strings with an offset) keep their moment. Example yuga.tz("2024-05-15 10:20", "Asia/Tokyo").toISOString(); // "2024-05-15T01:20:00.000Z" Throws When the time zone is unknown.
tz.guess () => string The system’s time zone id ("America/New_York").
tz.setDefault (zone?) => void The time zone of new instances that don’t set one (undefined resets to the system’s). A zone is kept even when it is the system’s, it doesn’t follow runtime changes to TZ. Throws When the time zone is unknown.
duration(value?, unit?): yuga.Duration;

A duration of a number of a unit (milliseconds by default), an object, an ISO 8601 string, a Temporal.Duration or another duration, Day.js’s duration plugin.

A number is balanced into parts (duration(90, "minutes") is 1 hour 30 minutes), an object or string keeps its parts as given (duration({ minutes: 90 }).minutes() is 90).

Passing a duration is a yugajs addition (Day.js gives NaN).

Parameter Type
value? yuga.DurationInput
unit? yuga.Unit

yuga.Duration

yuga.duration({ hours: 1, minutes: 30 }).asMinutes(); // 90
extend<T>(extension, ...args): void;

Extend an extension into the yuga class.

Type Parameter Default type
T never
Parameter Type
extension yuga.Extension<T>
…args [T] extends [never] ? [] : [T] extends [undefined] ? [] : undefined extends T ? [config?: T] : [config: T]

void

isDuration(value): value is yuga.Duration;

Whether a value is a duration.

Parameter Type
value unknown

value is yuga.Duration

isYuga(that): that is yuga.Yuga;

Check if a value is an Yuga class.

Parameter Type
that unknown

that is yuga.Yuga

locale(): string;

Get the global locale’s name ("en").

string

locale(preset, object?): string;

Set the global locale (the one new instances use) and return its name. Names are any BCP 47 tag (built out of Intl, lowercased: "en-GB" is "en-gb") or registered locales, a name with an object registers the object (overriding Intl), and a locale object registers it under its own name. Unknown locales keep the global one.

Parameter Type
preset string | yuga.Locale
object? yuga.Locale

string

locale(
preset,
object,
isLocal
): string | undefined;

Resolve (and register) a locale without setting the global one, its name or undefined when unknown.

Parameter Type
preset string | yuga.Locale
object yuga.Locale | undefined
isLocal true

string | undefined

localeData(): yuga.LocaleData;

The data of the global locale (Day.js’s localeData). Its months() and siblings ignore an instance and return the list of names.

yuga.LocaleData

max(...instances): yuga.Yuga | null;

The latest of the instances (or parsables), the same object that was passed in (the first one on ties). null with no input, and an invalid instance as soon as one input is invalid.

Parameter Type
…instances yuga.Parsable[]

yuga.Yuga | null

max(instances): yuga.Yuga | null;
Parameter Type
instances yuga.Parsable[]

yuga.Yuga | null

median(...instances): yuga.Yuga | null;

The middle instance by time, the same object that was passed in. With an even count it’s the earlier of the two middle ones (so it’s always one of the inputs), and the first one on ties (equal instances keep their input order, so the middle one of three equal inputs is the second). null with no input, and an invalid instance as soon as one input is invalid.

Parameter Type
…instances yuga.Parsable[]

yuga.Yuga | null

median(instances): yuga.Yuga | null;
Parameter Type
instances yuga.Parsable[]

yuga.Yuga | null

min(...instances): yuga.Yuga | null;

The earliest of the instances (or parsables), the same object that was passed in (the first one on ties). null with no input, and an invalid instance as soon as one input is invalid.

Parsables are a yugajs addition, Day.js’s minMax throws on non-instances.

Parameter Type
…instances yuga.Parsable[]

yuga.Yuga | null

min(instances): yuga.Yuga | null;
Parameter Type
instances yuga.Parsable[]

yuga.Yuga | null

months(): string[];

The month names of the global locale, January first.

string[]

monthsShort(): string[];

The short month names of the global locale.

string[]

plug<T>(extension, ...args): void;

Plug a plugin into the yuga factory.

Type Parameter Default type
T never
Parameter Type
extension yuga.Plugin<T>
…args [T] extends [never] ? [] : [T] extends [undefined] ? [] : [config: T]

void

unix(value): yuga.Yuga;

Create a Yuga object from a Unix timestamp, 10 digits, seconds since the Unix Epoch.

Parameter Type
value number | bigint

yuga.Yuga

updateLocale(name, partial): yuga.Locale | undefined;

Merges a partial locale into a loaded locale (a locale already used, registered, or en), so instances in it (new or existing) see the change, and returns it. Does nothing (and returns undefined) for a locale that isn’t loaded, the same as Day.js.

Objects (formats) are merged, anything else is replaced. Names of Intl locales are replaced too, with plain properties.

yuga.updateLocale("en", { months: ["Jan.", "Feb.", …] });
Parameter Type
name string
partial Partial<yuga.Locale>

yuga.Locale | undefined

utc(
parsable?,
format?,
locale?,
strict?
): yuga.Yuga;

Create an instance in UTC, the same as yuga(parsable, format, locale, strict) with utc: true (Day.js’s dayjs.utc). A format needs yugajs/extension/custom-parse-format.

Parameter Type
parsable? yuga.Parsable
format? string
locale? string | boolean | yuga.Locale
strict? boolean

yuga.Yuga

weekdays(localeOrder?): string[];

The weekday names of the global locale, Sunday first, or starting on the locale’s first day of the week with localeOrder.

Parameter Type
localeOrder? boolean

string[]

weekdaysMin(localeOrder?): string[];

The minimal weekday names of the global locale (see weekdays).

Parameter Type
localeOrder? boolean

string[]

weekdaysShort(localeOrder?): string[];

The short weekday names of the global locale (see weekdays).

Parameter Type
localeOrder? boolean

string[]

Property Type
$ yuga.Utils

A locale’s data, Day.js’s localeData object.

firstDayOfWeek(): number;

The first day of the week, 0 is Sunday.

number

longDateFormat(token): string | undefined;

The template of a localized format ("LT", "LTS", "L", "LL", "LLL", "LLLL", and their short forms), undefined for any other token.

Parameter Type
token string

string | undefined

meridiem(
hour,
minute,
isLowercase
): string;

The meridiem of an hour ("AM" / "PM" by default).

Parameter Type
hour number
minute number
isLowercase boolean

string

months(): string[];

The month names, January first. With an instance, that instance’s month name (MMMM).

string[]

months(instance): string;
Parameter Type
instance yuga.Yuga

string

monthsShort(): string[];

The short month names. With an instance, that instance’s short month name (MMM).

string[]

monthsShort(instance): string;
Parameter Type
instance yuga.Yuga

string

ordinal(number, token?): string | number;

The ordinal of a number ("1st" in English).

Parameter Type
number number
token? string

string | number

weekdays(): string[];

The weekday names, Sunday first. With an instance, that instance’s weekday name (dddd).

string[]

weekdays(instance): string;
Parameter Type
instance yuga.Yuga

string

weekdaysMin(): string[];

The minimal weekday names. With an instance, that instance’s minimal weekday name (dd).

string[]

weekdaysMin(instance): string;
Parameter Type
instance yuga.Yuga

string

weekdaysShort(): string[];

The short weekday names. With an instance, that instance’s short weekday name (ddd).

string[]

weekdaysShort(instance): string;
Parameter Type
instance yuga.Yuga

string


Property Type
d "day"
D "date"
h "hour"
m "minute"
M "month"
ms "millisecond"
q "quarter"
s "second"
w "week"
y "year"

Property Type Inherited from
d number yuga.Fields.d
D number yuga.Fields.D
h number yuga.Fields.h
m number yuga.Fields.m
M number yuga.Fields.M
ms number yuga.Fields.ms
q number yuga.Fields.q
s number yuga.Fields.s
w number yuga.Fields.w
y number yuga.Fields.y
add(value, unit?): yuga.Yuga;

Adds an amount of a unit (milliseconds by default), Day.js’s add.

  • y, Q and M move the wall clock and clamp the date to the end of the month (Jan 31 + 1 month is Feb 29 in 2024), fractions are truncated.
  • w and d (or D) move the wall clock by whole days (fractions are rounded), keeping the time of day across DST.
  • h, m, s and ms are absolute time (an hour across DST is 60 minutes later).

Units are case-insensitive. Lowercase q is a quarter (Day.js adds milliseconds).

Parameter Type
value number
unit? yuga.Unit

yuga.Yuga

yuga("2024-01-31").add(1, "month"); // 2024-02-29
add(value, unit?): yuga.Yuga;

Adds several units in the order of the object’s keys ({ months: 1, days: 1 } and { days: 1, months: 1 } differ on January 30th).

Available with yugajs/extension/object-support.

Parameter Type
value | number | Partial<Record<yuga.Unit, number>>
unit? yuga.Unit

yuga.Yuga

add(duration): yuga.Yuga;

Adds a duration: years and months, then weeks and days, then the time, the same as adding each part.

Day.js drops a duration’s weeks, yugajs adds them.

Parameter Type
duration number | yuga.Duration

yuga.Yuga

calendar(referenceTime?, formats?): string;

The instance in calendar words relative to a reference time ("Today at 10:20 AM", "Last Monday at 9:00 AM", "05/15/2024"), Day.js’s calendar plugin. The bucket is the day difference from the start of the reference’s day, in the instance’s time zone.

Parameter Type Description
referenceTime? yuga.Parsable What to compare with, now by default.
formats? CalendarFormats The format of each bucket, a template or a function; they win over the locale’s.

string

clone(): yuga.Yuga;

Creates a clone of the current Yuga class.

Yuga doesn’t support mutability, this function is ported from Moment.js & Day.js. While Day.js does default to immutability, plugins such as badMutable allow it, Yuga does not provide such plugins.

yuga.Yuga

date(): number;

number

date(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

dates(): number;

An alias of date(), the day of the month.

number

dates(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

day(): number;

number

day(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

dayOfYear(): number;

The day of the year in the instance’s time zone, 1 to 366 (Day.js’s dayOfYear plugin). The setter moves to that day of the year, the same as add(n - dayOfYear(), "day").

number

dayOfYear(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

days(): number;

An alias of day(), the day of the week.

number

days(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

daysInMonth(): number;

Get the number of days in the current month.

NaN when invalid.

number

diff(
that?,
unit?,
options?
): number;

The difference from a parsable (now when omitted) to the instance in a unit, milliseconds by default. NaN when either is invalid (for every unit, Day.js gives 0 for months).

By default it matches Day.js: months, quarters and years use Moment.js’s month difference (2024-05-15 to 2024-01-31 is 3.48 months), days and weeks discount the change of the UTC offset, and the result is truncated toward zero.

The parsable is compared in the instance’s time zone, the same as Moment.js.

Parameter Type Description
that? yuga.Parsable -
unit? yuga.Unit -
options? boolean | yuga.DiffOptions true keeps the fraction (Day.js’s float), see DiffOptions.

number

yuga("2024-05-15").diff("2024-01-31", "month", true); // 3.48…
endOf(unit): yuga.Yuga;

The end of a unit, the start of the next one minus 1 ms (endOf("day") is 23:59:59.999).

Parameter Type
unit yuga.Unit | "isoWeek"

yuga.Yuga

startOf

format(template?): string;

Formats with a template, Day.js’s format and its advancedFormat tokens, in the instance’s locale. Text in brackets is escaped ("[Today is] dddd").

  • YY YYYY M MM MMM MMMM D DD Do d dd ddd dddd H HH h hh a A m mm s ss SSS Z ZZ Q
  • w ww the week of the locale (its weekStart and yearStart), W WW the ISO week.

With no template it is ISO 8601 with the offset ("2024-05-15T06:00:00-04:00", and "…Z" in UTC), invalid instances are the locale’s invalidDate ("Invalid Date").

Parameter Type
template? string

string

yuga("2024-05-15").format("dddd, MMMM Do YYYY"); // "Wednesday, May 15th 2024"
from(date?, withoutSuffix?): string;

The time from a date to the instance in words, yuga("2024-01-01").from("2024-01-03") is "2 days ago".

Parameter Type
date? yuga.Parsable
withoutSuffix? boolean

string

fromNow(withoutSuffix?): string;

The time from now to the instance in words ("2 hours ago", "in a day"), Day.js’s relativeTime plugin. Past is “ago”, future is “in”.

Parameter Type Description
withoutSuffix? boolean Drop the “ago” / “in” ("2 hours").

string

get(): string;

Get the string value of the class.

Equivalent to yuga().toString()

string

get(unit): number;

Get the numeric value of the provided unit.

Equivalent to yuga()unit.

Parameter Type Description
unit yuga.Unit The unit of the desired value to get.

number

hour(): number;

number

hour(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

hours(): number;

An alias of hour().

number

hours(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

isAfter(that, unit?): boolean;

Checks if the instance is after a parsable, with a unit: if the parsable is before the start of the instance’s unit.

The unit is in the instance’s time zone and locale (see startOf).

Parameter Type
that yuga.Parsable
unit? yuga.Unit | "isoWeek"

boolean

isBefore(that, unit?): boolean;

Checks if the instance is before a parsable, with a unit: if the end of the instance’s unit is before it (isBefore(x, "day") is true when x is on a later day).

The unit is in the instance’s time zone and locale (see startOf).

Parameter Type
that yuga.Parsable
unit? yuga.Unit | "isoWeek"

boolean

isBetween(
a,
b,
unit?,
inclusivity?
): boolean;

Checks if the instance is between two parsables, given in either order. Exclusive by default ("()"), "[" and "]" include the lower and upper bound ("[]", "[)", "(]"). With a unit the comparison is by unit (see isBefore and isAfter).

Parameter Type
a yuga.Parsable
b yuga.Parsable
unit? yuga.Unit | "isoWeek"
inclusivity? "()" | "[]" | "[)" | "(]"

boolean

isLeapYear(): boolean;

Checks if the instance’s year (in its time zone) is a leap year (Day.js’s isLeapYear plugin). false when invalid.

boolean

isoWeek(): number;

The ISO week of the year, an alias of week() for Day.js code.

number

isoWeek(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

isoWeekday(): number;

The ISO day of the week, Monday is 1 and Sunday is 7. The setter moves within the Monday to Sunday week.

number

isoWeekday(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

isoWeeks(): number;

An alias of isoWeek(), the ISO week of the year.

number

isoWeeks(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

isoWeeksInYear(): 52 | 53;

The number of ISO weeks in the year, 52 or 53.

52 | 53

isoWeekYear(): number;

The year the ISO week belongs to (December 30th 2024 is in 2025).

number

isSame(that, unit?): boolean;

Checks if the instance is the same moment as a parsable, with a unit: if the parsable is within the instance’s unit (isSame(x, "week") is the same Sunday to Saturday week in en).

The unit is in the instance’s time zone and locale (see startOf).

Parameter Type
that yuga.Parsable
unit? yuga.Unit | "isoWeek"

boolean

isSameOrAfter(that, unit?): boolean;

Checks if the instance is the same as or after a parsable (by unit, see isAfter).

Parameter Type
that yuga.Parsable
unit? yuga.Unit | "isoWeek"

boolean

isSameOrBefore(that, unit?): boolean;

Checks if the instance is the same as or before a parsable (by unit, see isBefore).

Parameter Type
that yuga.Parsable
unit? yuga.Unit | "isoWeek"

boolean

isToday(): boolean;

Checks if the instance is today, in its time zone (Day.js’s isToday plugin).

Day.js compares with the system’s time zone, yugajs with the instance’s, the same as isSame(…, "day"). It’s false when invalid.

boolean

isTomorrow(): boolean;

Checks if the instance is tomorrow, in its time zone (Day.js’s isTomorrow plugin).

boolean

isUTC(): boolean;

Check if the instance is in UTC (the "UTC" time zone, and its names "Etc/UTC", "Etc/GMT" and "+00:00").

boolean

isValid(): boolean;

Get the validity of the class, opposite of “Invalid Date” in the JS Date object.

Equivalent to !isNaN(yuga().$e).

boolean

isYesterday(): boolean;

Checks if the instance is yesterday, in its time zone (Day.js’s isYesterday plugin).

boolean

local(keepLocalTime?): yuga.Yuga;

The same moment in the system’s time zone.

Parameter Type Description
keepLocalTime? boolean Keep the wall clock rather than the moment. A yugajs addition, mirroring utc(true) and tz(zone, true).

yuga.Yuga

locale(): string;

Get the name of the instance’s locale ("en").

string

locale(preset, object?): yuga.Yuga;

A clone in another locale, Day.js’s locale. Names are any BCP 47 tag (built out of Intl) or registered locales, an object registers it under the name. An unknown locale keeps the current one.

The global locale stays as is, see yuga.locale.

Parameter Type
preset string | yuga.Locale
object? yuga.Locale

yuga.Yuga

yuga("2024-05-15").locale("fr").format("dddd D MMMM"); // "mercredi 15 mai"
localeData(): yuga.LocaleData;

The data of the instance’s locale: names, first day of the week, formats, meridiem and ordinal. months(instance) and its siblings return the name for that instance.

Available with yugajs/extension/locale-data.

yuga.LocaleData

millisecond(): number;

Get the current millisecond.

number

millisecond(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

milliseconds(): number;

An alias of millisecond().

number

milliseconds(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

minute(): number;

number

minute(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

minutes(): number;

An alias of minute().

number

minutes(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

month(): number;

number

month(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

months(): number;

An alias of month().

number

months(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

offsetName(type?): string;

The name of the instance’s time zone at its moment, always in en-US like Day.js: "EDT" ("short", the default) or "Eastern Daylight Time" ("long").

Throws a RangeError when invalid, the same as Day.js. UTC instances are "UTC" (Day.js gives the system’s name).

Parameter Type
type? "short" | "long"

string

quarter(): number;

number

quarter(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

quarters(): number;

An alias of quarter().

number

quarters(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

second(): number;

number

second(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

seconds(): number;

An alias of second().

number

seconds(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

set(unit, value): yuga.Yuga;

Set the numeric value of the provided unit.

Equivalent to yuga()[unit](value).

In the repeated hour of a DST fall back the instance’s offset is kept when it’s valid for the new wall clock (set("minute", 0) of the second 01:30 is the second 01:00), the same as Temporal’s with and Luxon. Other wall clocks the time zone repeats are the earlier offset, and ones it skips move forward.

Parameter Type Description
unit yuga.Unit The unit of the desired value to set.
value number The new value.

yuga.Yuga

set(unit, value?): yuga.Yuga;

Sets several units in the order of the object’s keys (set({ hour: 1, minute: 2 }), no value).

Available with yugajs/extension/object-support.

Parameter Type
unit | yuga.Unit | Partial<Record<yuga.Unit, number>>
value? number

yuga.Yuga

startOf(unit): yuga.Yuga;

The start of a unit in the instance’s time zone (startOf("month") is the 1st at 00:00).

  • "week" starts on the weekStart of the instance’s locale (Sunday for en, Monday for en-gb), "isoWeek" always starts on Monday.
  • Calendar units are wall-clock midnights, a midnight the time zone skips moves forward.
  • h, m and s round the wall clock down, Temporal’s round with "floor": they keep the instance’s offset when it’s valid (the repeated hour of a DST fall back starts at its own 01:00, the same as Moment.js), and start at the top of the wall-clock hour across a 30-minute offset change (Lord Howe).

Milliseconds, unknown and missing units return the same moment.

Parameter Type
unit yuga.Unit | "isoWeek"

yuga.Yuga

subtract(value, unit?): yuga.Yuga;

Subtracts an amount of a unit, add with the value negated.

Parameter Type
value number
unit? yuga.Unit

yuga.Yuga

yuga("2024-03-31").subtract(1, "M"); // 2024-02-29
subtract(value, unit?): yuga.Yuga;

Subtracts several units in the order of the object’s keys.

Available with yugajs/extension/object-support.

Parameter Type
value | number | Partial<Record<yuga.Unit, number>>
unit? yuga.Unit

yuga.Yuga

subtract(duration): yuga.Yuga;

Subtracts a duration, add with every part negated.

Parameter Type
duration number | yuga.Duration

yuga.Yuga

to(date?, withoutSuffix?): string;

The time from the instance to a date in words, yuga("2024-01-01").to("2024-01-03") is "in 2 days".

Parameter Type
date? yuga.Parsable
withoutSuffix? boolean

string

toArray(): [number, number, number, number, number, number, number];

[year, month, date, hour, minute, second, millisecond] in the instance’s time zone, the month is 0-based (Day.js’s toArray plugin).

[number, number, number, number, number, number, number]

toDate(): Date;

Formats to a Date class equivalent value of the class.

Date

toInstant(): Instant;

The Temporal.Instant of the instance, throws a RangeError when invalid.

Instant

toISOString(keepOffset?): string;

Formats to an ISO 8601 string in UTC with milliseconds ("2024-05-15T10:00:00.000Z"), or at the instance’s own offset with keepOffset ("2024-05-15T12:00:00.000+02:00", Moment.js’s toISOString(true)).

Equivalent to yuga().toDate().toISOString(), throws a RangeError when invalid. For the time zone too, RFC 9557’s "2024-05-15T12:00:00+02:00[Europe/Paris]", use toTemporal().toString(): yuga() parses it back to the same instance.

Parameter Type
keepOffset? boolean

string

toJSON(): string | null;

Serializes to the ISO 8601 string, or null when invalid (used by JSON.stringify).

string | null

toNow(withoutSuffix?): string;

The time from the instance to now in words ("in 2 hours" for an instance in the past).

Parameter Type
withoutSuffix? boolean

string

toObject(): object;

The date and time fields in the instance’s time zone, the month is 0-based (Day.js’s toObject plugin).

object

Name Type
date number
hours number
milliseconds number
minutes number
months number
seconds number
years number
toPlainDate(): PlainDate;

The Temporal.PlainDate of the instance’s date in its time zone, throws a RangeError when invalid.

PlainDate

toString(): string;

Formats to a string in the instance’s time zone ("Wed May 15 2024 06:00:00 GMT-0400 (Eastern Daylight Time)"), “Invalid Date” when invalid.

Equivalent to yuga().toDate().toString() in the system’s time zone. Day.js returns the UTC string instead, see toUTCString.

string

toTemporal(): ZonedDateTime;

The Temporal.ZonedDateTime of the instance (its time zone, the system’s when it has none).

The same object as $T, built once. Throws a RangeError when invalid.

ZonedDateTime

toUTCString(): string;

Formats to a UTC string ("Wed, 15 May 2024 10:00:00 GMT"), “Invalid Date” when invalid.

Equivalent to yuga().toDate().toUTCString() and Day.js’s toString().

string

tz(): string;

Get the IANA time zone of the instance (the system’s when it has none of its own).

string

tz(zone, keepLocalTime?): yuga.Yuga;

The same moment in another time zone (an IANA name, or an offset such as "+05:30").

Parameter Type Description
zone string -
keepLocalTime? boolean Keep the wall clock rather than the moment (10:00 in New York becomes 10:00 in Tokyo).

yuga.Yuga

yuga("2024-05-15T10:00:00Z").tz("Asia/Tokyo").hour(); // 19

When the time zone is unknown.

unix(): number;

Get the number of seconds since the Unix Epoch, floored.

Equivalent to Math.floor(yuga().valueOf() / 1000).

number

utc(keepLocalTime?): yuga.Yuga;

The same moment in UTC, tz("UTC").

Parameter Type
keepLocalTime? boolean

yuga.Yuga

utcOffset(): number;

Get the offset from UTC in minutes, rounded to 15 minutes like Day.js (-240 for New York in summer).

NaN when invalid.

number

utcOffset(offset, keepLocalTime?): yuga.Yuga;

The same moment in a fixed offset from UTC, DST doesn’t apply to it.

Parameter Type Description
offset string | number Minutes (-240), hours when 16 or less in absolute value (-4), or a string ("-04:00", "+0530", an unparsable one returns the same instance). 0 is utc().
keepLocalTime? boolean Keep the wall clock rather than the moment.

yuga.Yuga

yuga("2024-05-15T10:00:00Z").utcOffset(-240).format(); // "2024-05-15T06:00:00-04:00"
valueOf(): number;

Get the number of milliseconds since the Unix Epoch of the Yuga class.

Equivalent to yuga().toDate().valueOf(), NaN when invalid.

number

week(): number;

number

week(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

weekday(): number;

The day of the week by the locale’s weekStart, 0 is the first day of the locale’s week (Sunday in en, Monday in en-gb). The setter moves within the week the same as day(n).

number

weekday(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

weeks(): number;

An alias of week().

number

weeks(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

weekYear(): number;

The year the week belongs to by the locale’s weekStart and yearStart.

number

year(): number;

number

year(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

years(): number;

An alias of year().

number

years(value): yuga.Yuga;
Parameter Type
value number

yuga.Yuga

Property Type Inherited from
$e number yuga.Fields.$e
$isYugaClass true yuga.Fields.$isYugaClass
$l string yuga.Fields.$l
$T ZonedDateTime | null yuga.Fields.$T
$v boolean yuga.Fields.$v
$z string | undefined yuga.Fields.$z
type "yuga.DurationInput" =
| number
| string
| yuga.DurationObject
| Temporal.Duration
| yuga.Duration;

What yuga.duration and duration.add take: a number (of milliseconds, or of a unit), an object, an ISO 8601 string ("P1DT2H"), a Temporal.Duration or another duration.


type "yuga.DurationObject" = { [K in yuga.Unit.Short | yuga.Unit.Long | yuga.Unit.Plural]?: number };

The parts of a duration by unit ({ hours: 1, minutes: 30 }), any spelling of a unit (hours, hour, h) the same as Day.js.


type "yuga.Extension"<TConfig> = (extensionConfig, yugaClass) => void;

An extension is used to extend the existing yuga structure.

Type Parameter
TConfig
Parameter Type
extensionConfig TConfig
yugaClass yuga.YugaClass

void


type "yuga.Options" = Omit<yuga.Config, "parsable">;

The options of yuga(parsable, options), a config without the parsable.


type "yuga.Parsable" = yuga.ExpandableParsable[keyof yuga.ExpandableParsable];

type "yuga.Plugin"<TConfig> = (pluginConfig, yugaFactory) => void;

A plugin is used to create separate structures to the yuga.

Type Parameter
TConfig
Parameter Type
pluginConfig TConfig
yugaFactory yuga.Factory

void


type "yuga.Unit" =
| yuga.Unit.Short
| yuga.Unit.Long
| yuga.Unit.Plural
| yuga.Unit.Loose;

Units are case-insensitive ("Year", "MONTHS"), except the short D (date) and M (month).


type "yuga.Unit.Long" = yuga.Unit.Map[keyof yuga.Unit.Map];

type "yuga.Unit.Loose" =
| Capitalize<
| yuga.Unit.Long
| yuga.Unit.Plural>
| Uppercase<
| yuga.Unit.Long
| yuga.Unit.Plural>
| "Q";

type "yuga.Unit.Plural" = `${yuga.Unit.Long}s`;

type "yuga.Unit.Short" = keyof yuga.Unit.Map;

type "yuga.UnitValues" = Partial<Record<yuga.Unit, number>>;

Values by unit, in the order they are applied. Inside a parsed object day and d are the date of the month (date and D too), as in Day.js.


type "yuga.YugaClass" = new (config) => yuga.Yuga;
Parameter Type
config yuga.Config

yuga.Yuga

const yuga: yuga.Factory;
Property Type Inherited from
d number yuga.Yuga.d
D number yuga.Yuga.D
date ((value) => yuga.Yuga) & (() => number) yuga.Fields.date
day ((value) => yuga.Yuga) & (() => number) yuga.Fields.day
h number yuga.Yuga.h
hour ((value) => yuga.Yuga) & (() => number) yuga.Fields.hour
m number yuga.Yuga.m
M number yuga.Yuga.M
millisecond ((value) => yuga.Yuga) & (() => number) yuga.Fields.millisecond
minute ((value) => yuga.Yuga) & (() => number) yuga.Fields.minute
month ((value) => yuga.Yuga) & (() => number) yuga.Fields.month
ms number yuga.Yuga.ms
q number yuga.Yuga.q
quarter ((value) => yuga.Yuga) & (() => number) yuga.Fields.quarter
s number yuga.Yuga.s
second ((value) => yuga.Yuga) & (() => number) yuga.Fields.second
w number yuga.Yuga.w
week ((value) => yuga.Yuga) & (() => number) yuga.Fields.week
y number yuga.Yuga.y
year ((value) => yuga.Yuga) & (() => number) yuga.Fields.year
Property Type Inherited from
$e number yuga.Yuga.$e
$isYugaClass true yuga.Yuga.$isYugaClass
$l string yuga.Yuga.$l
$T ZonedDateTime | null yuga.Yuga.$T
$v boolean yuga.Yuga.$v
$z string | undefined yuga.Yuga.$z

A locale, the same shape as Day.js’s locale objects (the parts format uses).

Locales are built out of Intl for any BCP 47 tag, en is built in. Optional names fall back to slicing the long ones, the same as Day.js ("Jan", "Sun", "Su").

Property Type Description
calendar? CalendarFormats The formats of calendar(), the same as Day.js’s locales’ calendar. They win over the words built from Intl. Available with yugajs/extension/calendar.
formats? Partial<Record<string, string>> The templates of the localized formats (LT, LTS, L, LL, LLL, LLLL), the same as Day.js’s locales’ formats. They win over the ones derived from Intl. A short form (l, ll, lll, llll) is its long one with shorter names (MMM, ddd) and numbers (M, D), unless it has a template of its own. Available with yugajs/extension/localized-format.
postformat? (output) => string Rewrites the result of format ("2024-05-15" to "٢٠٢٤-٠٥-١٥"), Day.js’s postformat. Needs the preParsePostFormat extension.
preparse? (input) => string Rewrites a string before it’s parsed ("٢٠٢٤-٠٥-١٥" to "2024-05-15"), Day.js’s preparse. Needs the preParsePostFormat extension.
relativeTime? RelativeTimeStrings The strings of relative time (fromNow, from, toNow, to), the same shape as Day.js’s relativeTime. Locales without it use Intl (en has Day.js’s English ones).
Property Type Description
invalidDate? string What invalid instances format to, "Invalid Date" by default.
meridiem? (hour, minute, isLowercase) => string The meridiem of an hour (A, and a when isLowercase), "AM" / "PM" by default.
months readonly string[] Month names, January first (MMMM).
monthsShort? readonly string[] Short month names (MMM), the first 3 letters of months by default.
name string The lowercase name the locale is registered under ("en", "en-gb").
ordinal (number, token?) => string | number The ordinal of a number (Do), "1st" in English. Day.js’s en wraps it in brackets ("[1st]"), those are removed when formatting.
weekdays readonly string[] Weekday names, Sunday first (dddd).
weekdaysMin? readonly string[] Minimal weekday names (dd), the first 2 letters of weekdays by default.
weekdaysShort? readonly string[] Short weekday names (ddd), the first 3 letters of weekdays by default.
weekStart? number The first day of the week, 0 is Sunday (the default) and 6 is Saturday.
yearStart? number The days of the new year its first week needs (w), 1 (the week of January 1st) by default. ISO weeks need 4 (the week of January 4th). Day.js’s yearStart (weekOfYear plugin), Moment.js’s 7 + dow - doy.

Property Modifier Type Description
tokensMap readonly Readonly<Record<string, string>> The regex of each built-in format parsing token, every token has a single capturing group. The order of the keys is the order of the alternation, longer tokens come first (YYYY before YY). Extensions add tokens with parseTokens instead. Formats are compiled once, changes only apply to formats not used yet. Available with yugajs/extension/custom-parse-format.
parseTwoDigitYear(input): number;

The year of a two-digit year (YY) as written, "69" is 1969 and "68" is 2068 by default (the same as Day.js and Moment.js). Replace it to change the pivot, Day.js’s parseTwoDigitYear option.

Available with yugajs/extension/custom-parse-format.

Parameter Type
input string

number

readonly format: string;

The default display format, ISO 8601 with the local offset (the same as Day.js).

“YYYY-MM-DDTHH:mm:ssZ”

readonly fromWall: (wall, timeZone) => number;

The epoch milliseconds of wall-clock values [y, M, D, h, m, s, ms] (see wall) in a time zone (undefined is the system’s), NaN when invalid. Overflowing values carry (M: 12 is January of the next year) and years 0-99 stay as written, the same as parse reads a string without an offset.

Parameter Type
wall number[]
timeZone string | undefined

number

readonly handlers: Map<object, {
add?: (instance, value, sign) => yuga.Yuga;
parse?: (config, timeZone) => number;
}>;

How core reads the values of extensions, by constructor. Each constructor has one handler, setting it again replaces it, so the order extensions are installed in doesn’t change any result.

  • parse reads parsables parse doesn’t read itself (Array for any array, otherwise the parsable’s constructor: Object for plain objects only), right before reading the rest as the Date constructor would. It returns the epoch milliseconds, NaN when invalid (or when it throws), and can call yuga.$.parse with a parsable core knows. A bigint is looked up by BigInt. undefined, null, numbers, strings, dates, instances, Temporal.ZonedDateTimes and Temporal.Instants never get here.
  • add moves an instance by an object value of add (sign 1) and subtract (sign -1), by the value’s constructor (Object for plain objects, a class). Numbers never get here, and objects without one are read as numbers.
yuga.$.handlers.set(Array, {
parse: (config, timeZone) => yuga.$.parse({ ...config, parsable: … }, timeZone),
});
yuga.$.handlers.set(Duration, {
add: (instance, duration: Duration, sign) => instance.add(duration.asMilliseconds() * sign),
});
readonly localeOf: (that) => yuga.Locale;

The locale of an instance, or of a config (its locale, the locale of the instance it parses, or the global one), en when unknown. Extensions use it rather than looking locales up.

Parameter Type
that | yuga.Yuga | yuga.Config

yuga.Locale

parseFormat?: (format, config) => string;

Rewrites each format of a string parsable before it is read (yugajs/extension/ localized-format expands L, LT…), unset by default. Called by yugajs/extension/custom-parse-format.

Parameter Type
format string
config yuga.Config

string

parseFormatted?: (input, config, timeZone) => number;

Reads a string parsable in the config’s format (each of them, when a list), the epoch milliseconds or NaN when invalid. Wall-clock values are read in timeZone, input is the string after preparse. parse calls it for every string with a format, they’re invalid when it is unset (the default). Set by yugajs/extension/custom-parse-format (Day.js’s customParseFormat).

Parameter Type
input string
config yuga.Config
timeZone string | undefined

number

parseString?: (input, config, timeZone) => number | undefined;

Reads a string parse can’t read itself (yugajs/extension/iso’s RFC 9557 strings, week dates…), unset by default. Strings that don’t match Day.js’s loose regex get here before Date reads them, strings in UTC or with an offset only when Date finds them invalid. It returns the epoch milliseconds (NaN when invalid), or undefined to leave the string to Date, and can call yuga.$.parse with another parsable.

Parameter Type
input string
config yuga.Config
timeZone string | undefined

number | undefined

parseTokens?: Record<string, [pattern: string, apply: (wall, raw, locale) => void]>;

The format parsing tokens of extensions, read by yugajs/extension/custom-parse-format, unset until one adds a token (yugajs/extension/weeks adds w and ww). Each token has the regex of its single capturing group, and how the text it matched changes the parsed wall-clock values ([y, M, D, h, m, s, ms], see wall), once the format’s own fields and their defaults are read (before A / a moves the hour). They come before the built-in tokens (tokensMap) in the alternation, in the order they’re added: a longer one must be added before a shorter one that shares its prefix. They don’t replace the built-in tokens. Either extension can be installed first.

// The week of the year (`weekOf` stands for the extension's code).
(yuga.$.parseTokens ??= {}).w = [
"(\\d{1,2})",
(wall, raw, locale) => (wall[2] += (+raw - weekOf(wall, locale)) * 7),
];

Formats are compiled once, tokens only apply to formats not used yet.

postformat?: (output, instance) => string;

Rewrites the result of format (Day.js’s postformat), unset by default. Tokens that format again (LT, L…) use the formatter they’re given (see formatToken), so only the outermost result goes through it.

Parameter Type
output string
instance yuga.Yuga

string

preparse?: (input, config) => string;

Rewrites a string parsable before parse reads it (Day.js’s preparse), unset by default. The config keeps the string as given.

Parameter Type
input string
config yuga.Config

string

readonly short: object;

Converts any unit into the short variation of it.

Name Type
d "d"
D "D"
date "D"
dates "D"
day "d"
days "d"
h "h"
hour "h"
hours "h"
m "m"
M "M"
millisecond "ms"
milliseconds "ms"
minute "m"
minutes "m"
month "M"
months "M"
ms "ms"
q "q"
quarter "q"
quarters "q"
s "s"
second "s"
seconds "s"
w "w"
week "w"
weeks "w"
y "y"
year "y"
years "y"
readonly unit: (name) => keyof Map | undefined;

The short unit of any unit spelling, case-insensitively ("Days" is "d", "M" is "M"), undefined when unknown. The same normalization as add, startOf, get… (see short).

Parameter Type
name string

keyof Map | undefined

readonly wall: object;

The index of each settable unit in the wall-clock values [y, M, D, h, m, s, ms], the same order as the Date constructor’s arguments (the month is 0-indexed).

Name Type
D 2
h 3
m 4
M 1
ms 6
s 5
y 0
readonly weekOfYear: (instance, weekStart, yearStart) => number;

The week of the year of an instance, for weeks starting on weekStart (0 is Sunday) whose first week of the year has at least yearStart days in it (a locale’s weekStart and yearStart, ISO weeks are 1 and 4). NaN when invalid.

Parameter Type
instance yuga.Yuga
weekStart number
yearStart number

number

zoneOf?: (config) => string | undefined;

The time zone of a parsable that isn’t an object (yugajs/extension/iso reads the RFC 9557 annotation of a string), unset by default. zone asks it only when the config has no zone and isn’t utc, undefined is the default time zone.

Parameter Type
config yuga.Config

string | undefined

cascade(yuga): yuga.Yuga;

Computes every cached unit field (y M D d h m s ms, then the UTC offset in minutes) from the instance’s $e in its $z, all of them are NaN when invalid. Instances call it on the first read of any of them, it returns the instance.

The system’s time zone and UTC are read off a Date, other time zones add their offset (cached between transitions) without building $T.

Parameter Type
yuga yuga.Mutable

yuga.Yuga

formatToken(token, formatter): void;

Adds a format token, or replaces one: format calls formatter with the instance and its locale. This is how plugins and extensions add tokens (e.g. k, the hour from 1 to 24):

yuga.$.formatToken("k", ({ h }) => h || 24);

Templates already used are compiled again, they may have read the token as literal text. Parsing doesn’t read the token (see parseTokens).

A token that stands for a template in each locale (LT is h:mm A in en) formats it with the format it’s given (instance.format would apply postformat to the inner result):

yuga.$.formatToken("LT", (instance, locale, format) =>
format(instance, templateOf(locale), locale)
);
Parameter Type
token string
formatter (instance, locale, format) => string | number

void

parse(config, timeZone): number;

Extracts the epoch milliseconds out of the provided config, NaN when invalid. Wall-clock values are read in timeZone (see zone).

  • undefined is now, null is invalid, numbers are epoch milliseconds.
  • Yuga instances, Temporal.ZonedDateTime and Temporal.Instant keep their epoch.
  • Strings with a format go to parseFormatted (yugajs/extension/custom-parse-format), they’re invalid without it. Strings without one use Day.js core’s loose YYYY-MM-DD HH:mm:ss.SSS regex as wall-clock values (unless they end in Z or an offset). Years 0000-0099 stay as is.
  • Strings go through preparse first, when set.
  • The parsables of extensions (arrays, plain objects, bigints, Temporal.PlainDate…) go to their handlers.
  • Everything else is read the same as the Date constructor, and is invalid where it would throw (a bigint or a Temporal.PlainDate without its extension).
Parameter Type
config yuga.Config
timeZone string | undefined

number

zone(config): string | undefined;

The time zone an instance of the config is in, undefined is the system’s.

config.zone comes first (validated, a RangeError when unknown), then Yuga instances and Temporal.ZonedDateTimes keep theirs, and config.utc is "UTC".

Parameter Type
config yuga.Config

string | undefined


type "yuga.Fields.Accessors" = Record<yuga.Unit.Long, () => number>;

type "yuga.Fields.Epoch" = "$e";

Milliseconds since the Unix Epoch, NaN when invalid (yuga(null), “Invalid Date”).


type "yuga.Fields.Locale" = "$l";

The name of the locale ("en"), the global locale when the instance was created.


type "yuga.Fields.Marker" = "$isYugaClass";

The marker of the class, made to check if a type provided is an instance of Yuga.

true


type "yuga.Fields.Mutators" = Record<yuga.Unit.Long, (value) => yuga.Yuga>;

type "yuga.Fields.Temporal" = "$T";

The zoned date time of $e in $z, null when invalid. Built on first access and cached.


type "yuga.Fields.Valid" = "$v";

type "yuga.Fields.Values" = Record<yuga.Unit.Short, number>;

type "yuga.Fields.Zone" = "$z";

The IANA time zone, undefined is the system’s (followed the same as Date does).


type "yuga.Mutable" = { -readonly [K in keyof yuga.Yuga]: yuga.Yuga[K] };