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

#PlainDateTimeComponents Source

type PlainDateTimeComponents :: Row Typetype PlainDateTimeComponents = (calendar :: String, day :: Int, era :: String, eraYear :: Int, hour :: Int, microsecond :: Int, millisecond :: Int, minute :: Int, month :: Int, monthCode :: String, nanosecond :: Int, second :: Int, year :: Int)

#fromWithOptions Source

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

Creates a PlainDateTime from component fields. Options: overflow.

exampleFromWithOptions :: Effect Unit
exampleFromWithOptions = do
  locale <- JS.Intl.Locale.new_ "en-US"
  dateTime <- PlainDateTime.fromWithOptions { overflow: Overflow.Constrain }
    { 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 rest. Union provided rest PlainDateTimeComponents => Record provided -> Effect PlainDateTime

Same as fromWithOptions withWithOptions default options.

exampleFrom :: Effect Unit
exampleFrom = do
  locale <- JS.Intl.Locale.new_ "en-US"
  dateTime <- PlainDateTime.from
    { 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

#fromStringWithOptions Source

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

exampleFromStringWithOptions :: Effect Unit
exampleFromStringWithOptions = do
  dateTime <- PlainDateTime.fromStringWithOptions { overflow: Overflow.Constrain } "2024-01-15T09:30:00"
  Console.log (PlainDateTime.toString dateTime)

2024-01-15T09:30:00

#fromString Source

fromString :: String -> Effect PlainDateTime

Same as fromStringWithOptions withWithOptions default options.

exampleFromString :: Effect Unit
exampleFromString = do
  locale <- JS.Intl.Locale.new_ "en-US"
  dateTime <- PlainDateTime.fromString "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

#year Source

year :: PlainDateTime -> Int

ISO calendar year number.

exampleYear :: Effect Unit
exampleYear = do
  dt <- PlainDateTime.fromString "2024-07-01T09:30:00"
  Console.log ("Year: " <> show (PlainDateTime.year dt))

Year: 2024

#month Source

month :: PlainDateTime -> Int

Month number within the year.

exampleMonth :: Effect Unit
exampleMonth = do
  dt <- PlainDateTime.fromString "2024-07-01T09:30:00"
  Console.log ("Month: " <> show (PlainDateTime.month dt))

Month: 7

#day Source

day :: PlainDateTime -> Int

Day of the month.

exampleDay :: Effect Unit
exampleDay = do
  dt <- PlainDateTime.fromString "2024-07-01T09:30:00"
  Console.log ("Day: " <> show (PlainDateTime.day dt))

Day: 1

#monthCode Source

monthCode :: PlainDateTime -> String

Calendar-specific month code, such as M01.

exampleMonthCode :: Effect Unit
exampleMonthCode = do
  dt <- PlainDateTime.fromString "2024-07-01T09:30:00"
  Console.log ("Month code: " <> PlainDateTime.monthCode dt)

Month code: M07

#dayOfWeek Source

dayOfWeek :: PlainDateTime -> Int

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

exampleDayOfWeek :: Effect Unit
exampleDayOfWeek = do
  dt <- PlainDateTime.fromString "2024-07-01T09:30:00"
  Console.log ("Day of week: " <> show (PlainDateTime.dayOfWeek dt))

Day of week: 1

#dayOfYear Source

dayOfYear :: PlainDateTime -> Int

Day number within the year.

exampleDayOfYear :: Effect Unit
exampleDayOfYear = do
  dt <- PlainDateTime.fromString "2024-07-01T09:30:00"
  Console.log ("Day of year: " <> show (PlainDateTime.dayOfYear dt))

Day of year: 183

#weekOfYear Source

weekOfYear :: PlainDateTime -> Maybe Int

Week number within the year when defined by the calendar.

exampleWeekOfYear :: Effect Unit
exampleWeekOfYear = do
  dt <- PlainDateTime.fromString "2024-07-01T09:30:00"
  Console.log ("Week of year: " <> show (PlainDateTime.weekOfYear dt))

Week of year: (Just 27)

#yearOfWeek Source

yearOfWeek :: PlainDateTime -> Maybe Int

Week-numbering year when defined by the calendar.

exampleYearOfWeek :: Effect Unit
exampleYearOfWeek = do
  dt <- PlainDateTime.fromString "2024-07-01T09:30:00"
  Console.log ("Year of week: " <> show (PlainDateTime.yearOfWeek dt))

Year of week: (Just 2024)

#daysInMonth Source

daysInMonth :: PlainDateTime -> Int

Number of days in the month.

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

Days in Feb 2024: 29

#daysInYear Source

daysInYear :: PlainDateTime -> Int

Number of days in the year.

exampleDaysInYear :: Effect Unit
exampleDaysInYear = do
  dt <- PlainDateTime.fromString "2024-07-01T00:00:00"
  Console.log ("Days in 2024: " <> show (PlainDateTime.daysInYear dt))

Days in 2024: 366

#daysInWeek Source

daysInWeek :: PlainDateTime -> Int

Number of days in the week for this calendar.

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

Days in week: 7

#monthsInYear Source

monthsInYear :: PlainDateTime -> Int

Number of months in the year for this calendar.

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

Months in year: 12

#inLeapYear Source

inLeapYear :: PlainDateTime -> Boolean

Whether the year is a leap year in this calendar.

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

2024 is leap year: true

#calendarId Source

calendarId :: PlainDateTime -> String

Calendar identifier, such as "iso8601".

exampleCalendarId :: Effect Unit
exampleCalendarId = do
  dt <- PlainDateTime.fromString "2024-07-01T00:00:00"
  Console.log ("Calendar: " <> PlainDateTime.calendarId dt)

Calendar: iso8601

#era Source

era :: PlainDateTime -> Maybe String

Era identifier when the calendar uses eras.

exampleEra :: Effect Unit
exampleEra = do
  dt <- PlainDateTime.fromString "2024-07-01T00:00:00"
  Console.log ("Era: " <> show (PlainDateTime.era dt))

Era: Nothing

#eraYear Source

eraYear :: PlainDateTime -> Maybe Int

Year number within the current era when available.

exampleEraYear :: Effect Unit
exampleEraYear = do
  dt <- PlainDateTime.fromString "2024-07-01T00:00:00"
  Console.log ("Era year: " <> show (PlainDateTime.eraYear dt))

Era year: Nothing

#hour Source

hour :: PlainDateTime -> Int

Hour component.

exampleHour :: Effect Unit
exampleHour = do
  dt <- PlainDateTime.fromString "2024-07-01T14:30:45"
  Console.log ("Hour: " <> show (PlainDateTime.hour dt))

Hour: 14

#minute Source

minute :: PlainDateTime -> Int

Minute component.

exampleMinute :: Effect Unit
exampleMinute = do
  dt <- PlainDateTime.fromString "2024-07-01T14:30:45"
  Console.log ("Minute: " <> show (PlainDateTime.minute dt))

Minute: 30

#second Source

second :: PlainDateTime -> Int

Second component.

exampleSecond :: Effect Unit
exampleSecond = do
  dt <- PlainDateTime.fromString "2024-07-01T14:30:45"
  Console.log ("Second: " <> show (PlainDateTime.second dt))

Second: 45

#millisecond Source

millisecond :: PlainDateTime -> Int

Millisecond component.

exampleMillisecond :: Effect Unit
exampleMillisecond = do
  dt <- PlainDateTime.fromString "2024-07-01T14:30:45.123"
  Console.log ("Millisecond: " <> show (PlainDateTime.millisecond dt))

Millisecond: 123

#microsecond Source

microsecond :: PlainDateTime -> Int

Microsecond component.

exampleMicrosecond :: Effect Unit
exampleMicrosecond = do
  dt <- PlainDateTime.fromString "2024-07-01T14:30:45.123456"
  Console.log ("Microsecond: " <> show (PlainDateTime.microsecond dt))

Microsecond: 456

#nanosecond Source

nanosecond :: PlainDateTime -> Int

Nanosecond component.

exampleNanosecond :: Effect Unit
exampleNanosecond = do
  dt <- PlainDateTime.fromString "2024-07-01T14:30:45.123456789"
  Console.log ("Nanosecond: " <> show (PlainDateTime.nanosecond dt))

Nanosecond: 789

#addWithOptions Source

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

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

exampleAddWithOptions :: Effect Unit
exampleAddWithOptions = do
  start <- PlainDateTime.fromString "2024-01-15T09:00:00"
  twoHours <- Duration.from { hours: 2 }
  end <- PlainDateTime.addWithOptions { overflow: Overflow.Constrain } twoHours start
  Console.log (PlainDateTime.toString end)

2024-01-15T11:00:00

#add Source

add :: Duration -> PlainDateTime -> Effect PlainDateTime

Same as addWithOptions with default options.

exampleAdd :: Effect Unit
exampleAdd = do
  start <- PlainDateTime.fromString "2024-01-15T09:00:00"
  twoHours <- Duration.from { hours: 2 }
  end <- PlainDateTime.add twoHours start
  Console.log (PlainDateTime.toString end)

2024-01-15T11:00:00

#subtractWithOptions Source

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

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

exampleSubtractWithOptions :: Effect Unit
exampleSubtractWithOptions = do
  start <- PlainDateTime.fromString "2024-01-15T11:00:00"
  twoHours <- Duration.from { hours: 2 }
  earlier <- PlainDateTime.subtractWithOptions { overflow: Overflow.Constrain } twoHours start
  Console.log (PlainDateTime.toString earlier)

2024-01-15T09:00:00

#subtract Source

subtract :: Duration -> PlainDateTime -> Effect PlainDateTime

Same as subtractWithOptions with default options.

exampleSubtract :: Effect Unit
exampleSubtract = do
  start <- PlainDateTime.fromString "2024-01-15T11:00:00"
  twoHours <- Duration.from { hours: 2 }
  earlier <- PlainDateTime.subtract twoHours start
  Console.log (PlainDateTime.toString earlier)

2024-01-15T09:00:00

#withWithOptions Source

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

exampleWithWithOptions :: Effect Unit
exampleWithWithOptions = do
  dateTime <- PlainDateTime.fromString "2024-01-15T09:30:45"
  noon <- PlainDateTime.withWithOptions { overflow: Overflow.Constrain } { hour: 12, minute: 0, second: 0 } dateTime
  Console.log (PlainDateTime.toString noon)

2024-01-15T12:00:00

#with Source

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

Same as withWithOptions with default options.

exampleWith :: Effect Unit
exampleWith = do
  dateTime <- PlainDateTime.fromString "2024-01-15T09:30:45"
  noon <- PlainDateTime.with { hour: 12, minute: 0, second: 0 } dateTime
  Console.log (PlainDateTime.toString noon)

2024-01-15T12:00:00

#withPlainTime Source

withPlainTime :: PlainTime -> PlainDateTime -> Effect PlainDateTime

Returns a new PlainDateTime with the time component replaced.

exampleWithPlainTime :: Effect Unit
exampleWithPlainTime = do
  dateTime <- PlainDateTime.fromString "2024-01-15T09:30:00"
  closingTime <- PlainTime.fromString "17:00:00"
  updated <- PlainDateTime.withPlainTime closingTime dateTime
  Console.log (PlainDateTime.toString updated)

2024-01-15T17:00:00

#withCalendar Source

withCalendar :: String -> PlainDateTime -> Effect PlainDateTime

Returns a new PlainDateTime that uses a different calendar.

exampleWithCalendar :: Effect Unit
exampleWithCalendar = do
  dateTime <- PlainDateTime.fromString "2019-05-01T09:30:00"
  japanese <- PlainDateTime.withCalendar "japanese" dateTime
  Console.log (PlainDateTime.calendarId japanese)

japanese

#untilWithOptions Source

untilWithOptions :: 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: untilWithOptions options other subject.

Options: largestUnit, smallestUnit, roundingIncrement, roundingMode.

exampleUntilWithOptions :: Effect Unit
exampleUntilWithOptions = do
  locale <- JS.Intl.Locale.new_ "en-US"
  now <- Now.plainDateTimeISO
    >>= PlainDateTime.round { smallestUnit: TemporalUnit.Second }
  nextBilling <- do
    aprilFirst <- PlainDateTime.from
      { year: PlainDateTime.year now, month: 4, day: 1 }
    if aprilFirst < now then do
      oneYear <- Duration.from { years: 1 }
      PlainDateTime.add oneYear aprilFirst
    else
      pure aprilFirst
  duration <- PlainDateTime.untilWithOptions
    { 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")

324 days until next billing

#until Source

until :: PlainDateTime -> PlainDateTime -> Effect Duration

Same as untilWithOptions with default options.

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

P74DT12H

#sinceWithOptions Source

sinceWithOptions :: forall provided. ConvertOptionsWithDefaults ToDifferenceOptions (Record DifferenceOptions) (Record provided) (Record DifferenceOptions) => Record provided -> PlainDateTime -> PlainDateTime -> 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"
  start <- PlainDateTime.fromString "2024-01-01T00:00:00"
  end <- PlainDateTime.fromString "2024-03-15T12:00:00"
  elapsed <- PlainDateTime.sinceWithOptions { 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 sinceWithOptions with default options.

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

P74DT12H

#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.

exampleRound :: Effect Unit
exampleRound = do
  dateTime <- PlainDateTime.fromString "2024-01-15T09:30:45.123"
  rounded <- PlainDateTime.round { smallestUnit: TemporalUnit.Minute } dateTime
  Console.log (PlainDateTime.toString rounded)

2024-01-15T09:31:00

#toStringWithOptions Source

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

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

exampleToStringWithOptions :: Effect Unit
exampleToStringWithOptions = do
  dateTime <- PlainDateTime.fromString "2024-01-15T09:30:00"
  Console.log (PlainDateTime.toStringWithOptions { smallestUnit: TemporalUnit.Minute } dateTime)

2024-01-15T09:30

#toString Source

toString :: PlainDateTime -> String

Default ISO 8601 serialization (no options).

exampleToString :: Effect Unit
exampleToString = do
  dateTime <- PlainDateTime.fromString "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.

exampleFromDateTime :: Effect Unit
exampleFromDateTime = do
  dt <- PlainDateTime.fromString "2024-07-01T12:00:00"
  roundTripped <- PlainDateTime.fromDateTime (PlainDateTime.toDateTime dt)
  Console.log (PlainDateTime.toString roundTripped)

2024-07-01T12:00:00

#toDateTime Source

toDateTime :: PlainDateTime -> DateTime

Converts a PlainDateTime to a purescript-datetime DateTime.

exampleToDateTime :: Effect Unit
exampleToDateTime = do
  dt <- PlainDateTime.fromString "2024-07-01T12:00:00"
  Console.log (show (PlainDateTime.toDateTime dt))

(DateTime (Date (Year 2024) July (Day 1)) (Time (Hour 12) (Minute 0) (Second 0) (Millisecond 0)))

#toPlainDate Source

toPlainDate :: PlainDateTime -> PlainDate

Extracts the date component.

exampleToPlainDate :: Effect Unit
exampleToPlainDate = do
  dateTime <- PlainDateTime.fromString "2024-01-15T09:30:00"
  Console.log (PlainDate.toString (PlainDateTime.toPlainDate dateTime))

2024-01-15

#toPlainTime Source

toPlainTime :: PlainDateTime -> PlainTime

Extracts the time component.

exampleToPlainTime :: Effect Unit
exampleToPlainTime = do
  dateTime <- PlainDateTime.fromString "2024-01-15T09:30:00"
  Console.log (PlainTime.toString (PlainDateTime.toPlainTime dateTime))

09:30:00

#toZonedDateTime Source

toZonedDateTime :: String -> PlainDateTime -> Effect ZonedDateTime

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

exampleToZonedDateTime :: Effect Unit
exampleToZonedDateTime = do
  plain <- PlainDateTime.fromString "2024-01-15T09:30:00"
  zoned <- PlainDateTime.toZonedDateTime "America/New_York" plain
  Console.log (ZonedDateTime.toString zoned)

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

Re-exports from JS.Temporal.PlainDateTime.Internal