Working with Dates/Times in Motoko

I see that there is a library that can return the current time in nanoseconds since epoch, but I don’t see anything that lets you do useful things with this value. Can I easily do any of the following things entirely in Motoko?

  • Format the nanoseconds value as a locale-aware/readable string like January 5th 2021, 4:49:30 pm
  • Parse a string like January 5th 2021, 4:49:30 pm into the nanoseconds since epoch
  • Subtract two dates, returning the timespan between them (as years/months/hours/minutes etc)

Basically I’m looking for something like moment.js, but for Motoko. How do you work with Dates/Times in a convenient way in Motoko?

2 Likes

Javascript covers this for the frontend of course but the Motoko base library doesn’t yet. It’s a good feature request @hansl @stanley.jones , or you could certainly try tackling it yourself if you like.

I’ve got a bit on my plate at the moment, but would love to get involved with this since I have written a time library before.

3 Likes

Started hacking on a basic time library here:

There aren’t many features at the moment. Time permitting, I’ll develop this further. I’m open to collaboration with anyone else interested.

9 Likes

Is there any way to import your library as a package(as we import node libraries)?

One way to manage dependencies is to use Vessel:

You can then declare dependencies like this:

And import them like this:

1 Like

I could configure it. Appreciated your help.

1 Like