Module

JS.Temporal.PlainDate

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

A calendar date (year, month, day) without time or time zone. Use for date-only values (e.g. birthdays, all-day events). Uses ISO 8601 calendar by default.

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

#PlainDateComponents Source

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

#addWithOptions Source

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

Adds a duration to a date. Supports calendar durations. Options: overflow.

exampleAddWithOptions :: Effect Unit
exampleAddWithOptions = do
  locale <- JS.Intl.Locale.new_ "en-US"
  date <- PlainDate.fromString "2024-03-15"
  oneWeek <- Duration.from { weeks: 1 }
  later <- PlainDate.addWithOptions { overflow: Overflow.Constrain } oneWeek date
  formatter <- JS.Intl.DateTimeFormat.new [ locale ] { dateStyle: "long" }
  Console.log (JS.Intl.DateTimeFormat.format formatter later)

March 22, 2024

#add Source

add :: Duration -> PlainDate -> Effect PlainDate

Same as addWithOptions with default options.

exampleAdd :: Effect Unit
exampleAdd = do
  locale <- JS.Intl.Locale.new_ "en-US"
  date <- PlainDate.fromString "2024-03-15"
  oneWeek <- Duration.from { weeks: 1 }
  later <- PlainDate.add oneWeek date
  formatter <- JS.Intl.DateTimeFormat.new [ locale ] { dateStyle: "long" }
  Console.log (JS.Intl.DateTimeFormat.format formatter later)

March 22, 2024

#calendarId Source

calendarId :: PlainDate -> String

Identifier of the associated calendar (for example "iso8601").

exampleCalendarId :: Effect Unit
exampleCalendarId = do
  date <- PlainDate.fromString "2024-07-01"
  Console.log ("Calendar: " <> PlainDate.calendarId date)

Calendar: iso8601

#day Source

day :: PlainDate -> Int

Day of the month.

exampleDay :: Effect Unit
exampleDay = do
  date <- PlainDate.fromString "2024-07-01"
  Console.log ("Day: " <> show (PlainDate.day date))

Day: 1

#dayOfWeek Source

dayOfWeek :: PlainDate -> Int

ISO day of week, from 1 (Monday) to 7 (Sunday).

exampleDayOfWeek :: Effect Unit
exampleDayOfWeek = do
  date <- PlainDate.fromString "2024-07-01"
  Console.log ("Day of week: " <> show (PlainDate.dayOfWeek date))

Day of week: 1

#dayOfYear Source

dayOfYear :: PlainDate -> Int

Day number within the year.

exampleDayOfYear :: Effect Unit
exampleDayOfYear = do
  date <- PlainDate.fromString "2024-07-01"
  Console.log ("Day of year: " <> show (PlainDate.dayOfYear date))

Day of year: 183

#daysInMonth Source

daysInMonth :: PlainDate -> Int

Number of days in the current month.

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

Days in Feb 2024: 29

#daysInWeek Source

daysInWeek :: PlainDate -> Int

Number of days in the current week according to the calendar.

exampleDaysInWeek :: Effect Unit
exampleDaysInWeek = do
  date <- PlainDate.fromString "2024-07-01"
  Console.log ("Days in week: " <> show (PlainDate.daysInWeek date))

Days in week: 7

#daysInYear Source

daysInYear :: PlainDate -> Int

Number of days in the current year.

exampleDaysInYear :: Effect Unit
exampleDaysInYear = do
  date <- PlainDate.fromString "2024-07-01"
  Console.log ("Days in 2024: " <> show (PlainDate.daysInYear date))

Days in 2024: 366

#era Source

era :: PlainDate -> Maybe String

Calendar era name, if this calendar uses eras.

exampleEra :: Effect Unit
exampleEra = do
  date <- PlainDate.fromString "2024-07-01"
  Console.log ("Era: " <> show (PlainDate.era date))

Era: Nothing

#eraYear Source

eraYear :: PlainDate -> Maybe Int

Year number within the current era, if this calendar uses eras.

exampleEraYear :: Effect Unit
exampleEraYear = do
  date <- PlainDate.fromString "2024-07-01"
  Console.log ("Era year: " <> show (PlainDate.eraYear date))

Era year: Nothing

#fromWithOptions Source

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

Creates a PlainDate from component fields. Options: overflow.

exampleFromWithOptions :: Effect Unit
exampleFromWithOptions = do
  locale <- JS.Intl.Locale.new_ "en-US"
  date <- PlainDate.fromWithOptions { overflow: Overflow.Constrain } { year: 2024, month: 7, day: 1 }
  formatter <- JS.Intl.DateTimeFormat.new [ locale ] { dateStyle: "long" }
  Console.log (JS.Intl.DateTimeFormat.format formatter date)

July 1, 2024

#fromDate Source

fromDate :: Date -> Effect PlainDate

Converts a purescript-datetime Date to a PlainDate.

exampleFromDate :: Effect Unit
exampleFromDate = do
  date <- PlainDate.fromString "2024-07-01"
  roundTripped <- PlainDate.fromDate (PlainDate.toDate date)
  Console.log (PlainDate.toString roundTripped)

2024-07-01

#fromStringWithOptions Source

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

Creates a PlainDate from an RFC 9557 / ISO 8601 date string (e.g. "2024-01-15"). Options: overflow.

exampleFromStringWithOptions :: Effect Unit
exampleFromStringWithOptions = do
  date <- PlainDate.fromStringWithOptions { overflow: Overflow.Constrain } "2024-01-15"
  Console.log (PlainDate.toString date)

2024-01-15

#fromString Source

fromString :: String -> Effect PlainDate

Same as fromStringWithOptions with default options.

exampleFromString :: Effect Unit
exampleFromString = do
  locale <- JS.Intl.Locale.new_ "en-US"
  date <- PlainDate.fromString "2024-01-15"
  formatter <- JS.Intl.DateTimeFormat.new [ locale ] { dateStyle: "long" }
  Console.log (JS.Intl.DateTimeFormat.format formatter date)

January 15, 2024

#from Source

from :: forall provided rest. Union provided rest PlainDateComponents => Record provided -> Effect PlainDate

Same as fromWithOptions with default options.

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

July 1, 2024

#inLeapYear Source

inLeapYear :: PlainDate -> Boolean

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

exampleInLeapYear :: Effect Unit
exampleInLeapYear = do
  date <- PlainDate.fromString "2024-07-01"
  Console.log ("2024 is leap year: " <> show (PlainDate.inLeapYear date))

2024 is leap year: true

#month Source

month :: PlainDate -> Int

Calendar month number.

exampleMonth :: Effect Unit
exampleMonth = do
  date <- PlainDate.fromString "2024-07-01"
  Console.log ("Month: " <> show (PlainDate.month date))

Month: 7

#monthCode Source

monthCode :: PlainDate -> String

Calendar-specific month code (for example "M01").

exampleMonthCode :: Effect Unit
exampleMonthCode = do
  date <- PlainDate.fromString "2024-07-01"
  Console.log ("Month code: " <> PlainDate.monthCode date)

Month code: M07

#monthsInYear Source

monthsInYear :: PlainDate -> Int

Number of months in the current year.

exampleMonthsInYear :: Effect Unit
exampleMonthsInYear = do
  date <- PlainDate.fromString "2024-07-01"
  Console.log ("Months in year: " <> show (PlainDate.monthsInYear date))

Months in year: 12

#sinceWithOptions Source

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

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

exampleSinceWithOptions :: Effect Unit
exampleSinceWithOptions = do
  locale <- JS.Intl.Locale.new_ "en-US"
  start <- PlainDate.fromString "2024-01-01"
  end <- PlainDate.fromString "2024-03-15"
  elapsed <- PlainDate.sinceWithOptions { largestUnit: TemporalUnit.Day } start end
  formatter <- JS.Intl.DurationFormat.new [ locale ] { style: "long" }
  Console.log ("Elapsed: " <> JS.Intl.DurationFormat.format formatter elapsed)

Elapsed: 74 days

#since Source

since :: PlainDate -> PlainDate -> Effect Duration

Same as sinceWithOptions with default options.

exampleSince :: Effect Unit
exampleSince = do
  start <- PlainDate.fromString "2024-01-01"
  end <- PlainDate.fromString "2024-03-15"
  elapsed <- PlainDate.since start end
  Console.log (Duration.toString elapsed)

P74D

#subtractWithOptions Source

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

Subtracts a duration. Arg order: subtractWithOptions options duration subject. Options: overflow.

exampleSubtractWithOptions :: Effect Unit
exampleSubtractWithOptions = do
  locale <- JS.Intl.Locale.new_ "en-US"
  date <- PlainDate.fromString "2024-03-15"
  oneWeek <- Duration.from { weeks: 1 }
  earlier <- PlainDate.subtractWithOptions { overflow: Overflow.Constrain } oneWeek date
  formatter <- JS.Intl.DateTimeFormat.new [ locale ] { dateStyle: "long" }
  Console.log (JS.Intl.DateTimeFormat.format formatter earlier)

March 8, 2024

#subtract Source

subtract :: Duration -> PlainDate -> Effect PlainDate

Same as subtractWithOptions with default options.

exampleSubtract :: Effect Unit
exampleSubtract = do
  locale <- JS.Intl.Locale.new_ "en-US"
  date <- PlainDate.fromString "2024-03-15"
  oneWeek <- Duration.from { weeks: 1 }
  earlier <- PlainDate.subtract oneWeek date
  formatter <- JS.Intl.DateTimeFormat.new [ locale ] { dateStyle: "long" }
  Console.log (JS.Intl.DateTimeFormat.format formatter earlier)

March 8, 2024

#toDate Source

toDate :: PlainDate -> Date

Converts a PlainDate to a purescript-datetime Date.

exampleToDate :: Effect Unit
exampleToDate = do
  date <- PlainDate.fromString "2024-07-01"
  let d = PlainDate.toDate date
  Console.log ("PureScript Date year: " <> show (fromEnum (Date.year d)))

PureScript Date year: 2024

#toPlainDateTime Source

toPlainDateTime :: PlainTime -> PlainDate -> PlainDateTime

Combines a PlainTime with this date to form a PlainDateTime.

exampleToPlainDateTime :: Effect Unit
exampleToPlainDateTime = do
  date <- PlainDate.fromString "2024-01-15"
  time <- PlainTime.fromString "09:30:00"
  Console.log (PlainDateTime.toString (PlainDate.toPlainDateTime time date))

2024-01-15T09:30:00

#toPlainMonthDay Source

toPlainMonthDay :: PlainDate -> PlainMonthDay

Extracts the month and day.

exampleToPlainMonthDay :: Effect Unit
exampleToPlainMonthDay = do
  date <- PlainDate.fromString "2024-01-15"
  Console.log (PlainMonthDay.toString (PlainDate.toPlainMonthDay date))

01-15

#toZonedDateTimeWithPlainTime Source

toZonedDateTimeWithPlainTime :: String -> PlainTime -> PlainDate -> Effect ZonedDateTime

Converts to a ZonedDateTime at the given time in the given time zone.

exampleToZonedDateTimeWithPlainTime :: Effect Unit
exampleToZonedDateTimeWithPlainTime = do
  date <- PlainDate.fromString "2024-01-15"
  time <- PlainTime.fromString "09:30:00"
  zoned <- PlainDate.toZonedDateTimeWithPlainTime "America/New_York" time date
  Console.log (ZonedDateTime.toString zoned)

2024-01-15T09:30:00-05:00[America/New_York]

#toZonedDateTime Source

toZonedDateTime :: String -> PlainDate -> Effect ZonedDateTime

Converts to a ZonedDateTime at midnight in the given time zone.

exampleToZonedDateTime :: Effect Unit
exampleToZonedDateTime = do
  date <- PlainDate.fromString "2024-01-15"
  zoned <- PlainDate.toZonedDateTime "America/New_York" date
  Console.log (ZonedDateTime.toString zoned)

2024-01-15T00:00:00-05:00[America/New_York]

#toPlainYearMonth Source

toPlainYearMonth :: PlainDate -> PlainYearMonth

Extracts the year and month.

exampleToPlainYearMonth :: Effect Unit
exampleToPlainYearMonth = do
  date <- PlainDate.fromString "2024-01-15"
  Console.log (PlainYearMonth.toString (PlainDate.toPlainYearMonth date))

2024-01

#toStringWithOptions Source

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

Serializes to ISO 8601 date format. Options: calendarName.

exampleToStringWithOptions :: Effect Unit
exampleToStringWithOptions = do
  date <- PlainDate.fromString "2024-01-15"
  Console.log (PlainDate.toStringWithOptions { calendarName: CalendarName.Always } date)

2024-01-15[u-ca=iso8601]

#toString Source

toString :: PlainDate -> String

Same as toStringWithOptions with default options.

exampleToString :: Effect Unit
exampleToString = do
  date <- PlainDate.fromString "2024-01-15"
  Console.log (PlainDate.toString date)

2024-01-15

#untilWithOptions Source

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

Duration from subject (last arg) until other (second arg). Arg order: until options other subject. Options: largestUnit, smallestUnit, roundingIncrement, roundingMode.

exampleUntilWithOptions :: Effect Unit
exampleUntilWithOptions = do
  locale <- JS.Intl.Locale.new_ "en-US"
  today <- Now.plainDateISO
  futureDate <- PlainDate.fromString "2026-12-25"
  untilDuration <- PlainDate.untilWithOptions { smallestUnit: TemporalUnit.Day } futureDate today
  formatter <- JS.Intl.DurationFormat.new [ locale ] { style: "long" }
  Console.log (JS.Intl.DurationFormat.format formatter untilDuration <> " until Christmas 2026")

228 days until Christmas 2026

#until Source

until :: PlainDate -> PlainDate -> Effect Duration

Same as untilWithOptions with default options.

exampleUntil :: Effect Unit
exampleUntil = do
  start <- PlainDate.fromString "2024-01-01"
  end <- PlainDate.fromString "2024-03-15"
  duration <- PlainDate.until end start
  Console.log (Duration.toString duration)

P74D

#weekOfYear Source

weekOfYear :: PlainDate -> Maybe Int

Week number within the year, if the calendar defines week numbering.

exampleWeekOfYear :: Effect Unit
exampleWeekOfYear = do
  date <- PlainDate.fromString "2024-07-01"
  Console.log ("Week of year: " <> show (PlainDate.weekOfYear date))

Week of year: (Just 27)

#withWithOptions Source

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

Returns a new PlainDate with specified fields replaced. Because PlainDate is immutable, this is the way to "set" fields. Options: overflow.

exampleWithWithOptions :: Effect Unit
exampleWithWithOptions = do
  date <- PlainDate.fromString "2021-07-06"
  lastDay <- PlainDate.withWithOptions { overflow: Overflow.Constrain } { day: PlainDate.daysInMonth date } date
  Console.log (PlainDate.toString lastDay)

2021-07-31

#withCalendar Source

withCalendar :: String -> PlainDate -> Effect PlainDate

Returns a new PlainDate with the given calendar (for example "iso8601").

exampleWithCalendar :: Effect Unit
exampleWithCalendar = do
  date <- PlainDate.fromString "2024-01-15"
  gregory <- PlainDate.withCalendar "gregory" date
  Console.log (PlainDate.toStringWithOptions { calendarName: CalendarName.Always } gregory)

2024-01-15[u-ca=gregory]

#with Source

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

Same as withWithOptions with default options.

exampleWith :: Effect Unit
exampleWith = do
  date <- PlainDate.fromString "2021-07-06"
  lastDay <- PlainDate.with { day: PlainDate.daysInMonth date } date
  Console.log (PlainDate.toString lastDay)

2021-07-31

#year Source

year :: PlainDate -> Int

Calendar year.

exampleYear :: Effect Unit
exampleYear = do
  date <- PlainDate.fromString "2024-07-01"
  Console.log ("Year: " <> show (PlainDate.year date))

Year: 2024

#yearOfWeek Source

yearOfWeek :: PlainDate -> Maybe Int

Week-numbering year, if the calendar defines week numbering.

exampleYearOfWeek :: Effect Unit
exampleYearOfWeek = do
  date <- PlainDate.fromString "2024-07-01"
  Console.log ("Year of week: " <> show (PlainDate.yearOfWeek date))

Year of week: (Just 2024)

Re-exports from JS.Temporal.PlainDate.Internal