Skip to content

First steps

An instance is a moment in a time zone, with a locale. It never changes: every method that changes something returns a new instance.

yuga(); // Now.
yuga("2024-05-15 10:20"); // 10:20 in the system's time zone.
yuga("2024-05-15T10:20:00Z"); // An instant.
yuga(new Date());
yuga(1715768400000); // Milliseconds since the epoch.
yuga.utc("2024-05-15 10:20"); // 10:20 in UTC.
yuga.tz("2024-05-15 10:20", "Asia/Tokyo"); // 10:20 in Tokyo.

A string yuga can’t read gives an invalid instance rather than throwing:

yuga("not a date").isValid(); // false
yuga("not a date").format(); // "Invalid Date"
const day = yuga("2024-05-15 10:20");
day.year(); // 2024
day.month(); // 4, months count from 0 like Day.js
day.date(); // 15
day.day(); // 3, Wednesday
day.hour(); // 10
day.get("quarter"); // 2
const day = yuga("2024-01-31");
day.add(1, "month"); // 2024-02-29, the end of February
day.subtract(2, "weeks");
day.startOf("month"); // 2024-01-01 00:00
day.month(1); // February: 2024-02-29
day.set("hour", 9);

day is still the 31st of January after all of these.

yuga("2024-05-15").format("dddd, MMMM Do YYYY"); // "Wednesday, May 15th 2024"
yuga("2024-05-15").locale("fr").format("dddd D MMMM"); // "mercredi 15 mai"
yuga.utc("2024-05-15 10:20").format(); // "2024-05-15T10:20:00Z"
  • Coming from Day.js? Migrate from Day.js: the calls are the same, under a new name.
  • The guides go through each area in detail.