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.
Create
Section titled “Create”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(); // falseyuga("not a date").format(); // "Invalid Date"const day = yuga("2024-05-15 10:20");
day.year(); // 2024day.month(); // 4, months count from 0 like Day.jsday.date(); // 15day.day(); // 3, Wednesdayday.hour(); // 10day.get("quarter"); // 2Change
Section titled “Change”const day = yuga("2024-01-31");
day.add(1, "month"); // 2024-02-29, the end of Februaryday.subtract(2, "weeks");day.startOf("month"); // 2024-01-01 00:00day.month(1); // February: 2024-02-29day.set("hour", 9);day is still the 31st of January after all of these.
Format
Section titled “Format”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.