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

#add Source

add :: 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.

locale <- JS.Intl.Locale.new_ "en-US"
date <- PlainDate.from_ "2024-03-15"
oneWeek <- Duration.new { weeks: 1 }
later <- PlainDate.add { 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 add with default options.

#calendarId Source

calendarId :: PlainDate -> String

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

#day Source

day :: PlainDate -> Int

Day of the month.

#dayOfWeek Source

dayOfWeek :: PlainDate -> Int

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

#dayOfYear Source

dayOfYear :: PlainDate -> Int

Day number within the year.

#daysInMonth Source

daysInMonth :: PlainDate -> Int

Number of days in the current month.

#daysInWeek Source

daysInWeek :: PlainDate -> Int

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

#daysInYear Source

daysInYear :: PlainDate -> Int

Number of days in the current year.

#era Source

era :: PlainDate -> Maybe String

Calendar era name, if this calendar uses eras.

#eraYear Source

eraYear :: PlainDate -> Maybe Int

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

#from Source

from :: 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.

locale <- JS.Intl.Locale.new_ "en-US"
date <- PlainDate.from { overflow: Overflow.Constrain } "2024-01-15"
formatter <- JS.Intl.DateTimeFormat.new [ locale ] { dateStyle: "long" }
Console.log (JS.Intl.DateTimeFormat.format formatter date)
January 15, 2024

#fromDate Source

fromDate :: Date -> Effect PlainDate

Converts a purescript-datetime Date to a PlainDate.

#from_ Source

from_ :: String -> Effect PlainDate

Same as from with default options.

#inLeapYear Source

inLeapYear :: PlainDate -> Boolean

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

#month Source

month :: PlainDate -> Int

Calendar month number.

#monthCode Source

monthCode :: PlainDate -> String

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

#monthsInYear Source

monthsInYear :: PlainDate -> Int

Number of months in the current year.

#new Source

new :: Int -> Int -> Int -> Effect PlainDate

Creates a PlainDate from year, month, day (ISO calendar). Prefer from for most use cases. Throws if the date is invalid or out of representable range.

locale <- JS.Intl.Locale.new_ "en-US"
date <- PlainDate.new 2024 7 1
formatter <- JS.Intl.DateTimeFormat.new [ locale ] { dateStyle: "long" }
Console.log (JS.Intl.DateTimeFormat.format formatter date)
July 1, 2024

#since Source

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

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

locale <- JS.Intl.Locale.new_ "en-US"
start <- PlainDate.from_ "2024-01-01"
end <- PlainDate.from_ "2024-03-15"
elapsed <- PlainDate.since { 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 since with default options.

#subtract Source

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

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

locale <- JS.Intl.Locale.new_ "en-US"
date <- PlainDate.from_ "2024-03-15"
oneWeek <- Duration.new { weeks: 1 }
earlier <- PlainDate.subtract { 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 subtract with default options.

#toDate Source

toDate :: PlainDate -> Date

Converts a PlainDate to a purescript-datetime Date.

#toPlainDateTime Source

toPlainDateTime :: PlainTime -> PlainDate -> PlainDateTime

Combines a PlainTime with this date to form a PlainDateTime.

date <- PlainDate.from_ "2024-01-15"
time <- PlainTime.from_ "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.

date <- PlainDate.from_ "2024-01-15"
Console.log (PlainMonthDay.toString_ (PlainDate.toPlainMonthDay date))
01-15

#toZonedDateTime Source

toZonedDateTime :: String -> PlainDate -> Effect ZonedDateTime

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

date <- PlainDate.from_ "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.

date <- PlainDate.from_ "2024-01-15"
Console.log (PlainYearMonth.toString_ (PlainDate.toPlainYearMonth date))
2024-01

#toString Source

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

Serializes to ISO 8601 date format. Options: calendarName.

date <- PlainDate.from_ "2024-01-15"
Console.log (PlainDate.toString { calendarName: CalendarName.Never } date)
2024-01-15

#toString_ Source

toString_ :: PlainDate -> String

Same as toString with default options.

#until Source

until :: 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.

locale <- JS.Intl.Locale.new_ "en-US"
today <- Now.plainDateISO_
futureDate <- PlainDate.from_ "2026-12-25"
untilDuration <- PlainDate.until { smallestUnit: TemporalUnit.Day } futureDate today
formatter <- JS.Intl.DurationFormat.new [ locale ] { style: "long" }
Console.log (JS.Intl.DurationFormat.format formatter untilDuration <> " until Christmas 2026")
292 days until Christmas 2026

#until_ Source

until_ :: PlainDate -> PlainDate -> Effect Duration

Same as until with default options.

#weekOfYear Source

weekOfYear :: PlainDate -> Maybe Int

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

#with Source

with :: 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.

locale <- JS.Intl.Locale.new_ "en-US"
date <- PlainDate.from_ "2021-07-06"
lastDayOfMonth <- PlainDate.with { overflow: Overflow.Constrain } { day: PlainDate.daysInMonth date } date
formatter <- JS.Intl.DateTimeFormat.new [ locale ] { dateStyle: "long" }
Console.log (JS.Intl.DateTimeFormat.format formatter lastDayOfMonth)
July 31, 2021

#withCalendar Source

withCalendar :: String -> PlainDate -> Effect PlainDate

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

date <- PlainDate.from_ "2024-01-15"
gregory <- PlainDate.withCalendar "gregory" date
Console.log (PlainDate.toString { 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 with with default options.

#year Source

year :: PlainDate -> Int

Calendar year.

#yearOfWeek Source

yearOfWeek :: PlainDate -> Maybe Int

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

Re-exports from JS.Temporal.PlainDate.Internal