DateTime and how to construct it

DateTime and how to construct it


DateTime is exported by purescript-datetime and represents a date/time value in the Gregorian calendar/UTC time zone.

data DateTime = DateTime Date Time

We can construct a DateTime value by simply passing a Date and a Time to it.

myDateTime :: DateTime Date Time
myDateTime = DateTime myDate myTime


An Instant is a duration in milliseconds relative to the Unix epoch (1970-01-01 00:00:00 UTC).

The constructor is private, so to build one we can use:

instant :: Milliseconds -> Maybe Instant

-- e.g.
myInstant = instant $ Milliseconds 1000.0
-- > Just (Instant (Milliseconds 1000.0))

This function takes a Milliseconds value and tries to build an Instant.

The minimum acceptable value equates to the bottom DateTime (if we pass negative values to go before the year 1970) and the maximum acceptable value equates to the top DateTime.

If we already have a Date or a DateTime we can use:

fromDateTime :: DateTime -> Instant

fromDate :: Date -> Instant

fromDate uses the assumed time 00:00:00.

It’s also possible to convert back by using:

unInstant :: Instant -> Milliseconds

toDateTime :: Instant -> DateTime

-- e.g.
myDateTime = toDateTime <$> instant (Milliseconds 1000.0)
-- > Just (DateTime (Date (Year 1980) January (Day 1)) (Time (Hour 0)) (Minute 0) (Second 1) (Millisecond 0))