Module

JS.Temporal.PlainDateTime

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

A date and time (year, month, day, hour, minute, etc.) without time zone. Use for wall-clock times that are not tied to a specific instant (e.g. "Jan 15, 2024 at 3pm"). Combine with a time zone to get a ZonedDateTime.

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

#new Source

new :: forall provided rest. Union provided rest PlainDateTimeComponents => Record provided -> Effect PlainDateTime

Creates a PlainDateTime from component fields.

locale <- JS.Intl.Locale.new_ "en-US"
dateTime <- PlainDateTime.new
  { year: 2024
  , month: 1
  , day: 15
  , hour: 9
  , minute: 30
  , second: 0
  }
formatter <- JS.Intl.DateTimeFormat.new [ locale ] { dateStyle: "long", timeStyle: "medium" }
Console.log (JS.Intl.DateTimeFormat.format formatter dateTime)
January 15, 2024 at 9:30:00 AM

#from Source

from :: forall provided. ConvertOptionsWithDefaults ToOverflowOptions (Record OverflowOptions) (Record provided) (Record OverflowOptions) => Record provided -> String -> Effect PlainDateTime

Parses a date-time string (e.g. "2024-01-15T15:30:00"). Options: overflow.

locale <- JS.Intl.Locale.new_ "en-US"
dateTime <- PlainDateTime.from { overflow: Overflow.Constrain } "2024-01-15T09:30:00"
formatter <- JS.Intl.DateTimeFormat.new [ locale ] { dateStyle: "long", timeStyle: "medium" }
Console.log (JS.Intl.DateTimeFormat.format formatter dateTime)
January 15, 2024 at 9:30:00 AM

#from_ Source

from_ :: String -> Effect PlainDateTime

Same as from with default options.

#year Source

year :: PlainDateTime -> Int

ISO calendar year number.

#month Source

month :: PlainDateTime -> Int

Month number within the year.

#day Source

day :: PlainDateTime -> Int

Day of the month.

#monthCode Source

monthCode :: PlainDateTime -> String

Calendar-specific month code, such as M01.

#dayOfWeek Source

dayOfWeek :: PlainDateTime -> Int

Day of the week, from 1 (Monday) to 7 (Sunday).

#dayOfYear Source

dayOfYear :: PlainDateTime -> Int

Day number within the year.

#weekOfYear Source

weekOfYear :: PlainDateTime -> Maybe Int

Week number within the year when defined by the calendar.

#yearOfWeek Source

yearOfWeek :: PlainDateTime -> Maybe Int

Week-numbering year when defined by the calendar.

#daysInMonth Source

daysInMonth :: PlainDateTime -> Int

Number of days in the month.

#daysInYear Source

daysInYear :: PlainDateTime -> Int

Number of days in the year.

#daysInWeek Source

daysInWeek :: PlainDateTime -> Int

Number of days in the week for this calendar.

#monthsInYear Source

monthsInYear :: PlainDateTime -> Int

Number of months in the year for this calendar.

#inLeapYear Source

inLeapYear :: PlainDateTime -> Boolean

Whether the year is a leap year in this calendar.

#calendarId Source

calendarId :: PlainDateTime -> String

Calendar identifier, such as "iso8601".

#era Source

era :: PlainDateTime -> Maybe String

Era identifier when the calendar uses eras.

#eraYear Source

eraYear :: PlainDateTime -> Maybe Int

Year number within the current era when available.

#hour Source

hour :: PlainDateTime -> Int

Hour component.

#minute Source

minute :: PlainDateTime -> Int

Minute component.

#second Source

second :: PlainDateTime -> Int

Second component.

#millisecond Source

millisecond :: PlainDateTime -> Int

Millisecond component.

#microsecond Source

microsecond :: PlainDateTime -> Int

Microsecond component.

#nanosecond Source

nanosecond :: PlainDateTime -> Int

Nanosecond component.

#add Source

add :: forall provided. ConvertOptionsWithDefaults ToOverflowOptions (Record OverflowOptions) (Record provided) (Record OverflowOptions) => Record provided -> Duration -> PlainDateTime -> Effect PlainDateTime

Adds a duration. Arg order: add options duration subject. Options: overflow.

locale <- JS.Intl.Locale.new_ "en-US"
start <- PlainDateTime.from_ "2024-01-15T09:00:00"
twoHours <- Duration.new { hours: 2 }
end <- PlainDateTime.add { overflow: Overflow.Constrain } twoHours start
formatter <- JS.Intl.DateTimeFormat.new [ locale ] { dateStyle: "long", timeStyle: "medium" }
Console.log (JS.Intl.DateTimeFormat.format formatter end)
January 15, 2024 at 11:00:00 AM

#add_ Source

add_ :: Duration -> PlainDateTime -> Effect PlainDateTime

Same as add with default options.

#subtract Source

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

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

locale <- JS.Intl.Locale.new_ "en-US"
start <- PlainDateTime.from_ "2024-01-15T11:00:00"
twoHours <- Duration.new { hours: 2 }
earlier <- PlainDateTime.subtract { overflow: Overflow.Constrain } twoHours start
formatter <- JS.Intl.DateTimeFormat.new [ locale ] { dateStyle: "long", timeStyle: "medium" }
Console.log (JS.Intl.DateTimeFormat.format formatter earlier)
January 15, 2024 at 9:00:00 AM

#subtract_ Source

subtract_ :: Duration -> PlainDateTime -> Effect PlainDateTime

Same as subtract with default options.

#with Source

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

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

locale <- JS.Intl.Locale.new_ "en-US"
dateTime <- PlainDateTime.from_ "2024-01-15T09:30:45"
noon <- PlainDateTime.with { overflow: Overflow.Constrain } { hour: 12, minute: 0, second: 0 } dateTime
formatter <- JS.Intl.DateTimeFormat.new [ locale ] { dateStyle: "long", timeStyle: "medium" }
Console.log (JS.Intl.DateTimeFormat.format formatter noon)
January 15, 2024 at 12:00:00 PM

#with_ Source

with_ :: forall fields rest. Union fields rest WithFields => Record fields -> PlainDateTime -> Effect PlainDateTime

Same as with with default options.

#withPlainTime Source

withPlainTime :: PlainTime -> PlainDateTime -> Effect PlainDateTime

Returns a new PlainDateTime with the time component replaced.

locale <- JS.Intl.Locale.new_ "en-US"
dateTime <- PlainDateTime.from_ "2024-01-15T09:30:00"
closingTime <- PlainTime.from_ "17:00:00"
updated <- PlainDateTime.withPlainTime closingTime dateTime
formatter <- JS.Intl.DateTimeFormat.new [ locale ] { dateStyle: "long", timeStyle: "medium" }
Console.log (JS.Intl.DateTimeFormat.format formatter updated)
January 15, 2024 at 5:00:00 PM

#withCalendar Source

withCalendar :: String -> PlainDateTime -> Effect PlainDateTime

Returns a new PlainDateTime that uses a different calendar.

dateTime <- PlainDateTime.from_ "2019-05-01T09:30:00"
japanese <- PlainDateTime.withCalendar "japanese" dateTime
Console.log (PlainDateTime.calendarId japanese)
japanese

#until Source

until :: forall provided. ConvertOptionsWithDefaults ToDifferenceOptions (Record DifferenceOptions) (Record provided) (Record DifferenceOptions) => Record provided -> PlainDateTime -> PlainDateTime -> Effect Duration

Computes the duration from subject (last arg) until other (second arg). Positive when other is later. Arg order: until options other subject.

Options: largestUnit, smallestUnit, roundingIncrement, roundingMode.

locale <- JS.Intl.Locale.new_ "en-US"
now <- Now.plainDateTimeISO_
  >>= PlainDateTime.round { smallestUnit: TemporalUnit.Second }
nextBilling <- do
  aprilFirst <- PlainDateTime.new
    { year: PlainDateTime.year now
    , month: 4
    , day: 1
    }
  if aprilFirst < now then do
    oneYear <- Duration.new { years: 1 }
    PlainDateTime.add_ oneYear aprilFirst
  else
    pure aprilFirst

duration <- PlainDateTime.until
  { smallestUnit: TemporalUnit.Day }
  nextBilling
  now
durationFormatter <- JS.Intl.DurationFormat.new [ locale ] { style: JS.Intl.Options.DurationFormatStyle.Long }
Console.log (JS.Intl.DurationFormat.format durationFormatter duration <> " until next billing")
23 days until next billing

#until_ Source

until_ :: PlainDateTime -> PlainDateTime -> Effect Duration

Same as until with default options.

#since Source

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

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

locale <- JS.Intl.Locale.new_ "en-US"
start <- PlainDateTime.from_ "2024-01-01T00:00:00"
end <- PlainDateTime.from_ "2024-03-15T12:00:00"
elapsed <- PlainDateTime.since { largestUnit: TemporalUnit.Day } start end
formatter <- JS.Intl.DurationFormat.new [ locale ] { style: JS.Intl.Options.DurationFormatStyle.Long }
Console.log ("Elapsed: " <> JS.Intl.DurationFormat.format formatter elapsed)
Elapsed: 74 days, 12 hours

#since_ Source

since_ :: PlainDateTime -> PlainDateTime -> Effect Duration

Same as since with default options.

#round Source

round :: forall provided. ConvertOptionsWithDefaults ToRoundOptions (Record RoundOptions) (Record provided) (Record RoundOptions) => Record provided -> PlainDateTime -> Effect PlainDateTime

Rounds to a specified unit. Options: smallestUnit, roundingIncrement, roundingMode.

locale <- JS.Intl.Locale.new_ "en-US"
dateTime <- PlainDateTime.from_ "2024-01-15T09:30:45.123"
rounded <- PlainDateTime.round { smallestUnit: TemporalUnit.Minute } dateTime
formatter <- JS.Intl.DateTimeFormat.new [ locale ] { dateStyle: "long", timeStyle: "medium" }
Console.log (JS.Intl.DateTimeFormat.format formatter rounded)
January 15, 2024 at 9:31:00 AM

#toString Source

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

Serializes to ISO 8601 format. Options: fractionalSecondDigits, smallestUnit, roundingMode, calendarName.

dateTime <- PlainDateTime.from_ "2024-01-15T09:30:00"
Console.log (PlainDateTime.toString { smallestUnit: TemporalUnit.Minute } dateTime)
2024-01-15T09:30

#toString_ Source

toString_ :: PlainDateTime -> String

Default ISO 8601 serialization (no options). Prefer over toString {}.

dateTime <- PlainDateTime.from_ "2024-01-15T09:30:00"
Console.log (PlainDateTime.toString_ dateTime)
2024-01-15T09:30:00

#fromDateTime Source

fromDateTime :: DateTime -> Effect PlainDateTime

Converts a purescript-datetime DateTime to a PlainDateTime.

#toDateTime Source

toDateTime :: PlainDateTime -> DateTime

Converts a PlainDateTime to a purescript-datetime DateTime.

#toPlainDate Source

toPlainDate :: PlainDateTime -> PlainDate

Extracts the date component.

locale <- JS.Intl.Locale.new_ "en-US"
dateTime <- PlainDateTime.from_ "2024-01-15T09:30:00"
date <- pure (PlainDateTime.toPlainDate dateTime)
formatter <- JS.Intl.DateTimeFormat.new [ locale ] { dateStyle: "long" }
Console.log (JS.Intl.DateTimeFormat.format formatter date)
January 15, 2024

#toPlainTime Source

toPlainTime :: PlainDateTime -> PlainTime

Extracts the time component.

locale <- JS.Intl.Locale.new_ "en-US"
dateTime <- PlainDateTime.from_ "2024-01-15T09:30:00"
time <- pure (PlainDateTime.toPlainTime dateTime)
formatter <- JS.Intl.DateTimeFormat.new [ locale ] { timeStyle: "medium" }
Console.log (JS.Intl.DateTimeFormat.format formatter time)
9:30:00 AM

#toZonedDateTime Source

toZonedDateTime :: String -> PlainDateTime -> Effect ZonedDateTime

Interprets this date-time as occurring in the given time zone.

locale <- JS.Intl.Locale.new_ "en-US"
plain <- PlainDateTime.from_ "2024-01-15T09:30:00"
zoned <- PlainDateTime.toZonedDateTime "America/New_York" plain
formatter <- JS.Intl.DateTimeFormat.new [ locale ] { dateStyle: "long", timeStyle: "medium" }
Console.log (JS.Intl.DateTimeFormat.format formatter zoned)
January 15, 2024 at 9:30:00 AM

Re-exports from JS.Temporal.PlainDateTime.Internal