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
#new Source
new :: BigInt -> Effect InstantCreates an Instant from epoch nanoseconds.
locale <- JS.Intl.Locale.new_ "en-US"
instant <- Instant.new (BigInt.fromInt 0)
formatter <- JS.Intl.DateTimeFormat.new [ locale ] { dateStyle: "long", timeStyle: "medium", timeZone: "UTC" }
Console.log (JS.Intl.DateTimeFormat.format formatter instant)
January 1, 1970 at 12:00:00 AM
#from Source
from :: String -> Effect InstantParses an ISO 8601 instant string (e.g. "2024-01-15T12:00:00Z"). Throws on invalid input.
locale <- JS.Intl.Locale.new_ "en-US"
instant <- Instant.from "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.
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.
instant <- Instant.fromEpochNanoseconds (BigInt.fromInt 1000000000)
Console.log (Instant.toString_ instant)
1970-01-01T00:00:01Z
#epochMilliseconds Source
epochMilliseconds :: Instant -> NumberMilliseconds since the Unix epoch.
#epochNanoseconds Source
epochNanoseconds :: Instant -> BigIntNanoseconds since the Unix epoch.
#until Source
until :: 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: until options other subject.
locale <- JS.Intl.Locale.new_ "en-US"
earlier <- Instant.from "2020-01-09T00:00Z"
later <- Instant.from "2020-01-09T04:00Z"
result <- Instant.until { 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
#since Source
since :: forall provided. ConvertOptionsWithDefaults ToDifferenceOptions (Record DifferenceOptions) (Record provided) (Record DifferenceOptions) => Record provided -> Instant -> Instant -> Effect DurationDuration from other to subject (inverse of until). Arg order: since options other subject.
locale <- JS.Intl.Locale.new_ "en-US"
earlier <- Instant.from "2020-01-09T00:00Z"
later <- Instant.from "2020-01-09T04:00Z"
elapsed <- Instant.since { 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
#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.
instant <- Instant.from "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.
#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.
#toZonedDateTimeISO Source
toZonedDateTimeISO :: String -> Instant -> ZonedDateTimeConverts the instant to a ZonedDateTime in the given time zone (e.g. "America/New_York").
instant <- Instant.from "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]
#toString Source
toString :: 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.
instant <- Instant.from "2024-01-15T12:00:00.789Z"
Console.log
( Instant.toString
{ smallestUnit: TemporalUnit.Second
, timeZone: "UTC"
}
instant
)
2024-01-15T12:00:00+00:00
#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