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.
#fromWithOptions Source
fromWithOptions :: forall optsProvided provided rest. Union provided rest PlainDateTimeComponents => ConvertOptionsWithDefaults ToOverflowOptions (Record OverflowOptions) (Record optsProvided) (Record OverflowOptions) => Record optsProvided -> Record provided -> Effect PlainDateTimeCreates 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 PlainDateTimeSame 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 PlainDateTimeParses 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 PlainDateTimeSame 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 -> IntISO 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 -> IntMonth 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 -> IntDay 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 -> StringCalendar-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 -> IntDay 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 -> IntDay 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 IntWeek 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 IntWeek-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 -> IntNumber 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 -> IntNumber 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 -> IntNumber 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 -> IntNumber 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 -> BooleanWhether 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 -> StringCalendar 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 StringEra 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 IntYear 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 -> IntHour 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 -> IntMinute 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 -> IntSecond 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 -> IntMillisecond 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 -> IntMicrosecond 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 -> IntNanosecond 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 PlainDateTimeAdds 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 PlainDateTimeSame 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 PlainDateTimeSubtracts 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 PlainDateTimeSame 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 PlainDateTimeReturns 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 PlainDateTimeSame 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 PlainDateTimeReturns 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 PlainDateTimeReturns 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 DurationComputes 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 DurationSame 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 DurationDuration 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 DurationSame 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 PlainDateTimeRounds 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 -> StringSerializes 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 -> StringDefault 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 PlainDateTimeConverts 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 -> DateTimeConverts 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 -> PlainDateExtracts 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 -> PlainTimeExtracts 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 ZonedDateTimeInterprets 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]
#ToOverflowOptions Source
data ToOverflowOptionsInstances
ConvertOption ToOverflowOptions "overflow" Overflow StringConvertOption ToOverflowOptions "overflow" String String
#ToDifferenceOptions Source
data ToDifferenceOptionsInstances
ConvertOption ToDifferenceOptions "largestUnit" TemporalUnit StringConvertOption ToDifferenceOptions "largestUnit" String StringConvertOption ToDifferenceOptions "smallestUnit" TemporalUnit StringConvertOption ToDifferenceOptions "smallestUnit" String StringConvertOption ToDifferenceOptions "roundingIncrement" Int IntConvertOption ToDifferenceOptions "roundingMode" RoundingMode StringConvertOption ToDifferenceOptions "roundingMode" String String
#ToRoundOptions Source
data ToRoundOptionsInstances
ConvertOption ToRoundOptions "smallestUnit" TemporalUnit StringConvertOption ToRoundOptions "smallestUnit" String StringConvertOption ToRoundOptions "roundingIncrement" Int IntConvertOption ToRoundOptions "roundingMode" RoundingMode StringConvertOption ToRoundOptions "roundingMode" String String
#ToToStringOptions Source
data ToToStringOptionsInstances
ConvertOption ToToStringOptions "calendarName" CalendarName StringConvertOption ToToStringOptions "calendarName" String StringConvertOption ToToStringOptions "fractionalSecondDigits" Int ForeignConvertOption ToToStringOptions "fractionalSecondDigits" String ForeignConvertOption ToToStringOptions "smallestUnit" TemporalUnit StringConvertOption ToToStringOptions "smallestUnit" String StringConvertOption ToToStringOptions "roundingMode" RoundingMode StringConvertOption ToToStringOptions "roundingMode" String String
Re-exports from JS.Temporal.PlainDateTime.Internal
- Modules
- JS.
Temporal. Duration - JS.
Temporal. Duration. Internal - JS.
Temporal. Instant - JS.
Temporal. Instant. Internal - JS.
Temporal. Internal - JS.
Temporal. Now - JS.
Temporal. Options. CalendarName - JS.
Temporal. Options. Disambiguation - JS.
Temporal. Options. OffsetDisambiguation - JS.
Temporal. Options. Overflow - JS.
Temporal. Options. RoundingMode - JS.
Temporal. Options. TemporalUnit - JS.
Temporal. PlainDate - JS.
Temporal. PlainDate. Internal - JS.
Temporal. PlainDateTime - JS.
Temporal. PlainDateTime. Internal - JS.
Temporal. PlainMonthDay - JS.
Temporal. PlainMonthDay. Internal - JS.
Temporal. PlainTime - JS.
Temporal. PlainTime. Internal - JS.
Temporal. PlainYearMonth - JS.
Temporal. PlainYearMonth. Internal - JS.
Temporal. ZonedDateTime - JS.
Temporal. ZonedDateTime. Internal