Skip to content

Parsing

yuga(value) reads:

  • nothing or undefined: now;
  • a Date, a number of milliseconds since the epoch, or another instance;
  • a Temporal.ZonedDateTime (keeping its time zone) or a Temporal.Instant;
  • a string, as below.

null and strings it can’t read give an invalid instance, never an error. Check with isValid().

Without a format, strings are read the same way as in Day.js. Day.js’s loose YYYY-MM-DD HH:mm:ss.SSS is read as the wall clock in the instance’s time zone; a string with Z or an offset is an instant.

yuga("2024-05-15"); // Midnight, in the system's time zone.
yuga("2024-05-15 10:20"); // 10:20, in the system's time zone.
yuga("2024-05-15T10:20:30.123");
yuga("20240515T102030"); // The basic format.
yuga("2024-05-15T10:20:00Z"); // An instant.
yuga("2024-05-15T10:20:00+02:00"); // An instant at +02:00.

yuga.utc(value) and yuga.tz(value, zone) read the same strings as a wall clock in UTC or in that time zone:

yuga.utc("2024-05-15 10:20").format(); // "2024-05-15T10:20:00Z"
yuga.tz("2024-05-15 10:20", "Asia/Tokyo").format(); // "2024-05-15T10:20:00+09:00"

With the custom-parse-format extension, a format as the second argument reads the string with the same tokens as format. A third argument is the locale for month and weekday names, or true for strict parsing.

import { customParseFormat } from "yugajs/extension/custom-parse-format";
yuga.extend(customParseFormat);
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, February has no 30th

The literal text of a format has to match the string. Day.js skips over separators that don’t match; yuga gives an invalid instance.

The iso extension reads the ISO 8601 and RFC 9557 forms Temporal reads, which core leaves out to stay small:

import { iso } from "yugajs/extension/iso";
yuga.extend(iso);
yuga("2024-05-15T10:20:30+02:00[Europe/Paris]").tz(); // "Europe/Paris"
yuga("2024-W20-3"); // Wednesday of week 20.
yuga("2024-136"); // The 136th day of 2024.

Without it, ordinal dates such as 2024-136 are invalid rather than read as a month 13.

Arrays, objects, bigints and Temporal.PlainDates each have their own extension, so apps only load the ones they use: array-support, object-support, big-int-support and plain-date-support. See Extensions and plugins.