Skip to content

Get and set

Each unit is a method: called without an argument it reads the unit, called with one it returns a copy with that unit set.

const day = yuga("2024-05-15 10:20");
day.year(); // 2024
day.month(); // 4, months count from 0
day.date(); // 15, the day of the month
day.day(); // 3, the day of the week (0 is Sunday)
day.hour(); // 10
day.minute(); // 20
day.second(); // 0
day.millisecond(); // 0
day.quarter(); // 2
day.week(); // 20, the ISO week

get and set take the unit by name, in any of Day.js’s spellings ("month", "months", "M"):

yuga("2024-05-15").get("quarter"); // 2
yuga("2024-05-15").set("month", 0); // 2024-01-15

Setting a month keeps the day where it can and clamps it at the end of a shorter month:

yuga("2024-01-31").month(1).date(); // 29

Other units roll over into the next one, so date(32) in May is the 1st of June, and hour(-1) is 23:00 the day before.

Setting a unit is an operation on the wall clock in the instance’s time zone. In the hour repeated when clocks go back, yuga keeps the offset the instance had when it’s still valid, the same as Temporal’s with.

  • daysInMonth() is the number of days in the instance’s month.
  • The weeks extension adds the locale’s week (weekday, weekYear) and the ISO week year.
  • day-of-year, is-leap-year and plural-get-set (years(), months()…) add what their names say.