Module

JS.Temporal.PlainYearMonth

Package
purescript-js-temporal
Repository
pete-murphy/purescript-js-temporal

A year and month without day or time zone. Use for month-level values (e.g. "January 2024"). Requires a reference day for some operations.

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainYearMonth

#PlainYearMonthComponents Source

type PlainYearMonthComponents :: Row Typetype PlainYearMonthComponents = (calendar :: String, era :: String, eraYear :: Int, month :: Int, monthCode :: String, year :: Int)

#fromWithOptions Source

fromWithOptions :: forall optsProvided provided rest. Union provided rest PlainYearMonthComponents => ConvertOptionsWithDefaults ToOverflowOptions (Record OverflowOptions) (Record optsProvided) (Record OverflowOptions) => Record optsProvided -> Record provided -> Effect PlainYearMonth

Creates a PlainYearMonth from component fields. Options: overflow.

exampleFromWithOptions :: Effect Unit
exampleFromWithOptions = do
  ym <- PlainYearMonth.fromWithOptions { overflow: Overflow.Constrain } { year: 2024, month: 6 }
  Console.log (PlainYearMonth.toString ym)

2024-06

#from Source

from :: forall provided rest. Union provided rest PlainYearMonthComponents => Record provided -> Effect PlainYearMonth

Same as fromWithOptions with default options.

exampleFrom :: Effect Unit
exampleFrom = do
  locale <- JS.Intl.Locale.new_ "en-US"
  yearMonth <- PlainYearMonth.from { year: 2024, month: 6 }
  firstDay <- PlainYearMonth.toPlainDate { day: 1 } yearMonth
  formatter <- JS.Intl.DateTimeFormat.new [ locale ] { dateStyle: "long" }
  Console.log (JS.Intl.DateTimeFormat.format formatter firstDay)

June 1, 2024

#fromStringWithOptions Source

fromStringWithOptions :: forall provided. ConvertOptionsWithDefaults ToOverflowOptions (Record OverflowOptions) (Record provided) (Record OverflowOptions) => Record provided -> String -> Effect PlainYearMonth

Creates a PlainYearMonth from an RFC 9557 / ISO 8601 year-month string (e.g. "2024-01"). Options: overflow.

exampleFromStringWithOptions :: Effect Unit
exampleFromStringWithOptions = do
  ym <- PlainYearMonth.fromStringWithOptions { overflow: Overflow.Constrain } "2024-06"
  Console.log (PlainYearMonth.toString ym)

2024-06

#fromString Source

fromString :: String -> Effect PlainYearMonth

Same as fromStringWithOptions with default options.

exampleFromString :: Effect Unit
exampleFromString = do
  yearMonth <- PlainYearMonth.fromString "2024-06"
  Console.log (PlainYearMonth.toString yearMonth)

2024-06

#year Source

year :: PlainYearMonth -> Int

ISO calendar year number.

exampleYear :: Effect Unit
exampleYear = do
  ym <- PlainYearMonth.fromString "2024-06"
  Console.log ("Year: " <> show (PlainYearMonth.year ym))

Year: 2024

#month Source

month :: PlainYearMonth -> Int

Month number within the year.

exampleMonth :: Effect Unit
exampleMonth = do
  ym <- PlainYearMonth.fromString "2024-06"
  Console.log ("Month: " <> show (PlainYearMonth.month ym))

Month: 6

#monthCode Source

monthCode :: PlainYearMonth -> String

Calendar-specific month code, such as M06.

exampleMonthCode :: Effect Unit
exampleMonthCode = do
  ym <- PlainYearMonth.fromString "2024-06"
  Console.log ("Month code: " <> PlainYearMonth.monthCode ym)

Month code: M06

#daysInMonth Source

daysInMonth :: PlainYearMonth -> Int

Number of days in the represented month.

exampleDaysInMonth :: Effect Unit
exampleDaysInMonth = do
  ym <- PlainYearMonth.fromString "2024-02"
  Console.log ("Days in Feb 2024: " <> show (PlainYearMonth.daysInMonth ym))

Days in Feb 2024: 29

#daysInYear Source

daysInYear :: PlainYearMonth -> Int

Number of days in the represented year.

exampleDaysInYear :: Effect Unit
exampleDaysInYear = do
  ym <- PlainYearMonth.fromString "2024-06"
  Console.log ("Days in 2024: " <> show (PlainYearMonth.daysInYear ym))

Days in 2024: 366

#monthsInYear Source

monthsInYear :: PlainYearMonth -> Int

Number of months in the represented year for this calendar.

exampleMonthsInYear :: Effect Unit
exampleMonthsInYear = do
  ym <- PlainYearMonth.fromString "2024-06"
  Console.log ("Months in year: " <> show (PlainYearMonth.monthsInYear ym))

Months in year: 12

#inLeapYear Source

inLeapYear :: PlainYearMonth -> Boolean

Whether the represented year is a leap year in this calendar.

exampleInLeapYear :: Effect Unit
exampleInLeapYear = do
  ym <- PlainYearMonth.fromString "2024-06"
  Console.log ("2024 is leap year: " <> show (PlainYearMonth.inLeapYear ym))

2024 is leap year: true

#calendarId Source

calendarId :: PlainYearMonth -> String

Calendar identifier, such as "iso8601".

exampleCalendarId :: Effect Unit
exampleCalendarId = do
  ym <- PlainYearMonth.fromString "2024-06"
  Console.log ("Calendar: " <> PlainYearMonth.calendarId ym)

Calendar: iso8601

#era Source

era :: PlainYearMonth -> Maybe String

Era identifier when the calendar uses eras.

exampleEra :: Effect Unit
exampleEra = do
  ym <- PlainYearMonth.fromString "2024-06"
  Console.log ("Era: " <> show (PlainYearMonth.era ym))

Era: Nothing

#eraYear Source

eraYear :: PlainYearMonth -> Maybe Int

Year number within the current era when available.

exampleEraYear :: Effect Unit
exampleEraYear = do
  ym <- PlainYearMonth.fromString "2024-06"
  Console.log ("Era year: " <> show (PlainYearMonth.eraYear ym))

Era year: Nothing

#addWithOptions Source

addWithOptions :: forall provided. ConvertOptionsWithDefaults ToOverflowOptions (Record OverflowOptions) (Record provided) (Record OverflowOptions) => Record provided -> Duration -> PlainYearMonth -> Effect PlainYearMonth

Adds a duration. Options: overflow.

exampleAddWithOptions :: Effect Unit
exampleAddWithOptions = do
  ym <- PlainYearMonth.fromString "2024-06"
  threeMonths <- Duration.from { months: 3 }
  later <- PlainYearMonth.addWithOptions { overflow: Overflow.Constrain } threeMonths ym
  Console.log (PlainYearMonth.toString later)

2024-09

#add Source

add :: Duration -> PlainYearMonth -> Effect PlainYearMonth

Same as addWithOptions with default options.

exampleAdd :: Effect Unit
exampleAdd = do
  ym <- PlainYearMonth.fromString "2024-06"
  threeMonths <- Duration.from { months: 3 }
  later <- PlainYearMonth.add threeMonths ym
  Console.log (PlainYearMonth.toString later)

2024-09

#subtractWithOptions Source

subtractWithOptions :: forall provided. ConvertOptionsWithDefaults ToOverflowOptions (Record OverflowOptions) (Record provided) (Record OverflowOptions) => Record provided -> Duration -> PlainYearMonth -> Effect PlainYearMonth

Subtracts a duration. Options: overflow.

exampleSubtractWithOptions :: Effect Unit
exampleSubtractWithOptions = do
  ym <- PlainYearMonth.fromString "2024-06"
  twoMonths <- Duration.from { months: 2 }
  earlier <- PlainYearMonth.subtractWithOptions { overflow: Overflow.Constrain } twoMonths ym
  Console.log (PlainYearMonth.toString earlier)

2024-04

#subtract Source

subtract :: Duration -> PlainYearMonth -> Effect PlainYearMonth

Same as subtractWithOptions with default options.

exampleSubtract :: Effect Unit
exampleSubtract = do
  ym <- PlainYearMonth.fromString "2024-06"
  twoMonths <- Duration.from { months: 2 }
  earlier <- PlainYearMonth.subtract twoMonths ym
  Console.log (PlainYearMonth.toString earlier)

2024-04

#withWithOptions Source

withWithOptions :: forall optsProvided fields rest. Union fields rest WithFields => ConvertOptionsWithDefaults ToOverflowOptions (Record OverflowOptions) (Record optsProvided) (Record OverflowOptions) => Record optsProvided -> Record fields -> PlainYearMonth -> Effect PlainYearMonth

Returns a new PlainYearMonth with specified fields replaced. Options: overflow.

exampleWithWithOptions :: Effect Unit
exampleWithWithOptions = do
  ym <- PlainYearMonth.fromString "2024-06"
  changed <- PlainYearMonth.withWithOptions { overflow: Overflow.Constrain } { month: 12 } ym
  Console.log (PlainYearMonth.toString changed)

2024-12

#with Source

with :: forall fields rest. Union fields rest WithFields => Record fields -> PlainYearMonth -> Effect PlainYearMonth

Same as withWithOptions with default options.

exampleWith :: Effect Unit
exampleWith = do
  ym <- PlainYearMonth.fromString "2024-06"
  changed <- PlainYearMonth.with { month: 12 } ym
  Console.log (PlainYearMonth.toString changed)

2024-12

#toPlainDate Source

toPlainDate :: { day :: Int } -> PlainYearMonth -> Effect PlainDate

Converts to PlainDate by supplying a day.

exampleToPlainDate :: Effect Unit
exampleToPlainDate = do
  locale <- JS.Intl.Locale.new_ "en-US"
  yearMonth <- PlainYearMonth.fromString "2024-01"
  firstDay <- PlainYearMonth.toPlainDate { day: 1 } yearMonth
  formatter <- JS.Intl.DateTimeFormat.new [ locale ] { dateStyle: "long" }
  Console.log (JS.Intl.DateTimeFormat.format formatter firstDay)

January 1, 2024

#untilWithOptions Source

untilWithOptions :: forall provided. ConvertOptionsWithDefaults ToDifferenceOptions (Record DifferenceOptions) (Record provided) (Record DifferenceOptions) => Record provided -> PlainYearMonth -> PlainYearMonth -> Effect Duration

Duration from subject (last arg) until other (second arg). Arg order: untilWithOptions options other subject.

exampleUntilWithOptions :: Effect Unit
exampleUntilWithOptions = do
  locale <- JS.Intl.Locale.new_ "en-US"
  start <- PlainYearMonth.fromString "2024-01"
  end <- PlainYearMonth.fromString "2025-06"
  duration <- PlainYearMonth.untilWithOptions { largestUnit: TemporalUnit.Year } end start
  formatter <- JS.Intl.DurationFormat.new [ locale ] { style: "long" }
  Console.log (JS.Intl.DurationFormat.format formatter duration)

1 year, 5 months

#until Source

until :: PlainYearMonth -> PlainYearMonth -> Effect Duration

Same as untilWithOptions with default options.

exampleUntil :: Effect Unit
exampleUntil = do
  start <- PlainYearMonth.fromString "2024-01"
  end <- PlainYearMonth.fromString "2025-06"
  duration <- PlainYearMonth.until end start
  Console.log (Duration.toString duration)

P1Y5M

#sinceWithOptions Source

sinceWithOptions :: forall provided. ConvertOptionsWithDefaults ToDifferenceOptions (Record DifferenceOptions) (Record provided) (Record DifferenceOptions) => Record provided -> PlainYearMonth -> PlainYearMonth -> Effect Duration

Duration from other to subject (inverse of untilWithOptions). Arg order: sinceWithOptions options other subject.

exampleSinceWithOptions :: Effect Unit
exampleSinceWithOptions = do
  locale <- JS.Intl.Locale.new_ "en-US"
  earlier <- PlainYearMonth.fromString "2022-06"
  later <- PlainYearMonth.fromString "2024-06"
  duration <- PlainYearMonth.sinceWithOptions { largestUnit: TemporalUnit.Year } earlier later
  formatter <- JS.Intl.DurationFormat.new [ locale ] { style: "long" }
  Console.log (JS.Intl.DurationFormat.format formatter duration)

2 years

#since Source

since :: PlainYearMonth -> PlainYearMonth -> Effect Duration

Same as sinceWithOptions with default options.

exampleSince :: Effect Unit
exampleSince = do
  earlier <- PlainYearMonth.fromString "2022-06"
  later <- PlainYearMonth.fromString "2024-06"
  duration <- PlainYearMonth.since earlier later
  Console.log (Duration.toString duration)

P2Y

#toStringWithOptions Source

toStringWithOptions :: forall provided. ConvertOptionsWithDefaults ToToStringOptions (Record ToStringOptions) (Record provided) (Record ToStringOptions) => Record provided -> PlainYearMonth -> String

Serializes to ISO 8601 year-month format. Options: calendarName.

exampleToStringWithOptions :: Effect Unit
exampleToStringWithOptions = do
  ym <- PlainYearMonth.fromString "2024-06"
  Console.log (PlainYearMonth.toStringWithOptions {} ym)

2024-06

#toString Source

toString :: PlainYearMonth -> String

Same as toStringWithOptions with default options.

exampleToString :: Effect Unit
exampleToString = do
  ym <- PlainYearMonth.fromString "2024-06"
  Console.log (PlainYearMonth.toString ym)

2024-06

Re-exports from JS.Temporal.PlainYearMonth.Internal