JS.Temporal.Instant
- Package
- purescript-js-temporal
- Repository
- pete-murphy/purescript-js-temporal
A point in time with nanosecond precision, represented as nanoseconds since
the Unix epoch (1970-01-01T00:00:00Z). No time zone or calendar. Use
toZonedDateTimeISO to interpret in a time zone.
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/Instant
#fromString Source
fromString :: String -> Effect InstantParses an ISO 8601 instant string (e.g. "2024-01-15T12:00:00Z"). Throws on invalid input.
exampleFromString :: Effect Unit
exampleFromString = do
locale <- JS.Intl.Locale.new_ "en-US"
instant <- Instant.fromString "2024-01-15T12:00:00Z"
formatter <- JS.Intl.DateTimeFormat.new [ locale ] { dateStyle: "long", timeStyle: "medium", timeZone: "UTC" }
Console.log (JS.Intl.DateTimeFormat.format formatter instant)
January 15, 2024 at 12:00:00 PM
#fromEpochMilliseconds Source
fromEpochMilliseconds :: Number -> Effect InstantCreates an Instant from epoch milliseconds.
exampleFromEpochMilliseconds :: Effect Unit
exampleFromEpochMilliseconds = do
instant <- Instant.fromEpochMilliseconds 1000.0
Console.log (Instant.toString instant)
1970-01-01T00:00:01Z
#fromEpochNanoseconds Source
fromEpochNanoseconds :: BigInt -> Effect InstantCreates an Instant from epoch nanoseconds.
exampleFromEpochNanoseconds :: Effect Unit
exampleFromEpochNanoseconds = do
instant <- Instant.fromEpochNanoseconds (BigInt.fromInt 1000000000)
Console.log (Instant.toString instant)
1970-01-01T00:00:01Z
#fromJSDate Source
fromJSDate :: JSDate -> Effect InstantCreates an Instant from a JavaScript Date.
exampleFromJSDate :: Effect Unit
exampleFromJSDate = do
jsDate <- JSDate.parse "2024-01-15T12:00:00Z"
instant <- Instant.fromJSDate jsDate
Console.log (Instant.toString instant)
2024-01-15T12:00:00Z
#epochMilliseconds Source
epochMilliseconds :: Instant -> NumberMilliseconds since the Unix epoch.
exampleEpochMilliseconds :: Effect Unit
exampleEpochMilliseconds = do
instant <- Instant.fromString "1970-01-01T00:00:01Z"
Console.log ("Epoch ms: " <> show (Instant.epochMilliseconds instant))
Epoch ms: 1000.0
#epochNanoseconds Source
epochNanoseconds :: Instant -> BigIntNanoseconds since the Unix epoch.
exampleEpochNanoseconds :: Effect Unit
exampleEpochNanoseconds = do
instant <- Instant.fromString "1970-01-01T00:00:01Z"
Console.log ("Epoch ns: " <> show (Instant.epochNanoseconds instant))
Epoch ns: 1000000000
#add Source
add :: Duration -> Instant -> Effect InstantAdds a duration to an instant. Throws for calendar durations.
exampleAdd :: Effect Unit
exampleAdd = do
instant <- Instant.fromString "2024-01-15T12:00:00Z"
oneHour <- Duration.from { hours: 1 }
later <- Instant.add oneHour instant
Console.log (Instant.toString later)
2024-01-15T13:00:00Z
#subtract Source
subtract :: Duration -> Instant -> Effect InstantSubtracts a duration. Arg order: subtract duration instant.
exampleSubtract :: Effect Unit
exampleSubtract = do
instant <- Instant.fromString "2024-01-15T12:00:00Z"
oneHour <- Duration.from { hours: 1 }
earlier <- Instant.subtract oneHour instant
Console.log (Instant.toString earlier)
2024-01-15T11:00:00Z
#untilWithOptions Source
untilWithOptions :: forall provided. ConvertOptionsWithDefaults ToDifferenceOptions (Record DifferenceOptions) (Record provided) (Record DifferenceOptions) => Record provided -> Instant -> Instant -> Effect DurationDuration from subject (last arg) until other (second arg). Arg order: untilWithOptions options other subject.
exampleUntilWithOptions :: Effect Unit
exampleUntilWithOptions = do
locale <- JS.Intl.Locale.new_ "en-US"
earlier <- Instant.fromString "2020-01-09T00:00Z"
later <- Instant.fromString "2020-01-09T04:00Z"
result <- Instant.untilWithOptions { largestUnit: TemporalUnit.Hour } later earlier
formatter <- JS.Intl.DurationFormat.new [ locale ] { style: "long" }
Console.log ("4 hours: " <> JS.Intl.DurationFormat.format formatter result)
4 hours: 4 hours
#until Source
until :: Instant -> Instant -> Effect DurationSame as untilWithOptions with default options.
exampleUntil :: Effect Unit
exampleUntil = do
earlier <- Instant.fromString "2020-01-09T00:00Z"
later <- Instant.fromString "2020-01-09T04:00Z"
result <- Instant.until later earlier
Console.log (Duration.toString result)
PT14400S
#sinceWithOptions Source
sinceWithOptions :: forall provided. ConvertOptionsWithDefaults ToDifferenceOptions (Record DifferenceOptions) (Record provided) (Record DifferenceOptions) => Record provided -> Instant -> Instant -> 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"
earlier <- Instant.fromString "2020-01-09T00:00Z"
later <- Instant.fromString "2020-01-09T04:00Z"
elapsed <- Instant.sinceWithOptions { largestUnit: TemporalUnit.Hour } earlier later
formatter <- JS.Intl.DurationFormat.new [ locale ] { style: "long" }
Console.log ("Elapsed: " <> JS.Intl.DurationFormat.format formatter elapsed)
Elapsed: 4 hours
#since Source
since :: Instant -> Instant -> Effect DurationSame as sinceWithOptions with default options.
exampleSince :: Effect Unit
exampleSince = do
earlier <- Instant.fromString "2020-01-09T00:00Z"
later <- Instant.fromString "2020-01-09T04:00Z"
elapsed <- Instant.since earlier later
Console.log (Duration.toString elapsed)
PT14400S
#round Source
round :: forall provided. ConvertOptionsWithDefaults ToRoundOptions (Record RoundOptions) (Record provided) (Record RoundOptions) => Record provided -> Instant -> Effect InstantRounds the instant to the given smallest unit. Options: smallestUnit, roundingIncrement, roundingMode.
exampleRound :: Effect Unit
exampleRound = do
instant <- Instant.fromString "2024-01-15T12:00:00.789Z"
rounded <- Instant.round
{ smallestUnit: TemporalUnit.Second
, roundingMode: RoundingMode.HalfExpand
}
instant
Console.log (Instant.toString rounded)
2024-01-15T12:00:01Z
#fromDateTimeInstant Source
fromDateTimeInstant :: Instant -> Effect InstantConverts a purescript-datetime Instant to a Temporal Instant.
exampleFromDateTimeInstant :: Effect Unit
exampleFromDateTimeInstant = do
let dtInstant = DateTime.Instant.fromDateTime bottom
instant <- Instant.fromDateTimeInstant dtInstant
Console.log (Instant.toString instant)
-271820-01-01T00:00:00Z
#toDateTimeInstant Source
toDateTimeInstant :: Instant -> Maybe InstantConverts a Temporal Instant to a purescript-datetime Instant.
Returns Nothing if the value is outside the datetime Instant range.
exampleToDateTimeInstant :: Effect Unit
exampleToDateTimeInstant = do
instant <- Instant.fromString "2024-01-15T12:00:00Z"
case Instant.toDateTimeInstant instant of
Just dtInstant -> Console.log (show dtInstant)
Nothing -> Console.log "Out of range"
(Instant (Milliseconds 1705320000000.0))
#toZonedDateTimeISO Source
toZonedDateTimeISO :: String -> Instant -> ZonedDateTimeConverts the instant to a ZonedDateTime in the given time zone (e.g. "America/New_York").
exampleToZonedDateTimeISO :: Effect Unit
exampleToZonedDateTimeISO = do
instant <- Instant.fromString "2024-01-15T12:00:00Z"
zoned <- pure (Instant.toZonedDateTimeISO "America/New_York" instant)
Console.log (ZonedDateTime.toString zoned)
2024-01-15T07:00:00-05:00[America/New_York]
#toStringWithOptions Source
toStringWithOptions :: forall provided. ConvertOptionsWithDefaults ToToStringOptions (Record ToStringOptions) (Record provided) (Record ToStringOptions) => Record provided -> Instant -> StringSerializes the instant to ISO 8601 format. Options: fractionalSecondDigits, smallestUnit, roundingMode, timeZone.
exampleToStringWithOptions :: Effect Unit
exampleToStringWithOptions = do
instant <- Instant.fromString "2024-01-15T12:00:00.789Z"
Console.log
( Instant.toStringWithOptions
{ smallestUnit: TemporalUnit.Second
, timeZone: "UTC"
}
instant
)
2024-01-15T12:00:00+00:00
#toString Source
toString :: Instant -> StringSame as toStringWithOptions with default options.
exampleToString :: Effect Unit
exampleToString = do
instant <- Instant.fromString "2024-01-15T12:00:00.789Z"
Console.log (Instant.toString instant)
2024-01-15T12:00:00.789Z
#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 "fractionalSecondDigits" Int ForeignConvertOption ToToStringOptions "fractionalSecondDigits" String ForeignConvertOption ToToStringOptions "smallestUnit" TemporalUnit StringConvertOption ToToStringOptions "smallestUnit" String StringConvertOption ToToStringOptions "roundingMode" RoundingMode StringConvertOption ToToStringOptions "roundingMode" String StringConvertOption ToToStringOptions "timeZone" String String
Re-exports from JS.Temporal.Instant.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