from

Creates a Month value from a canonical month string, date-like string, native Date, month object, or another Month value.

import { Month } from "@teakit/month";

Month.from("2025-01").toString(); // "2025-01"
Month.from("2025-01").year; // "2025"
Month.from("2025-01").month; // "01"
Month.from("2025-01-15").toString(); // "2025-01"
Month.from("2025-01-15").toString({ precision: "day" }); // "2025-01-15"
Month.from("2025-01-15T08:30:00.000Z").toString(); // "2025-01"
Month.from(new Date(2025, 0, 15)).toString(); // "2025-01"
Month.from(new Date(2025, 0, 15)).toString({ precision: "day" }); // "2025-01-15"
Month.from({ year: 2025, month: 12 }).toString(); // "2025-12"
Month.from({ year: "2025", month: "12" }).toString(); // "2025-12"
Month.from(Month.from("2025-07")).toString(); // "2025-07"

// Surrounding whitespace is accepted and output is canonical.
Month.from(" 2025-01 ").toString(); // "2025-01"

// Signed or expanded year strings are invalid.
Month.from("-0001-12"); // throws: invalid month string
Month.from("+10000-01"); // throws: invalid month string

API Reference

Signatures

Month.from(value: MonthValue): MonthInstance;
Month.fromIndex(index: number): MonthInstance;

Parameters

ParameterTypeRequiredNotes
valueMonthValueYesstring, Date, MonthInstance, or { year, month }. String inputs and object string years require exactly four year digits. Object month numbers accept 1..12; object month strings require "01" through "12".
indexnumberYesSafe integer serial month index where 0000-01 is 0.

Returns

Returns an immutable MonthInstance. Its year and month properties are strings; month is zero-padded, such as "01".

Throws

  • Throws for invalid month or date-like strings, including signed or expanded year strings.
  • Throws when a date-like string has a day that does not exist in its source month.
  • Throws for invalid Date objects.
  • Throws when numeric year, month, or index is not a safe integer.
  • Throws when month is outside 1..12.

Agent Contract

FieldValue
Kindstatic factory
Canonical namefrom
AliasesNone
Mutates receiverNo
ReturnsMonthInstance
Related methodsfromIndex, isMonth, toObject, toString

Agent Notes

  • Generate Month.from(...), not new Month(...) or Month(...).
  • Do not generate Month.from(2025, 1); the canonical factory takes one value argument.
  • Import with import { Month } from "@teakit/month"; default imports are unsupported.
  • Prefer canonical YYYY-MM strings with exactly four year digits and a zero-padded month.
  • Do not generate signed or expanded year strings such as "+10000-01" or "-0001-12".
  • Native Date inputs are read with local calendar fields. Prefer YYYY-MM or date-like strings when timezone-free behavior matters.
  • Date-like strings and native Date inputs preserve a day anchor for toString({ precision: "day" }); default toString() still returns YYYY-MM.
  • Use month.year and month.month only as display strings; use index or Month methods for calculations.
  • Do not use JavaScript Date parsing for string inputs.