How to convert Time.now() to "YYYY-MM-DD" in Motoko?

Hi there - I am trying to convert Time.now() to “YYYY-MM-DD” format, this is a key I use for many HashMaps on DSocial e.g. daily channel stats.

At the moment I am manually creating this on the front end and passing to my functions on the smart contract, this seems a bit dumb.

How could I convert Time.now() to a string date format of “YYYY-MM-DD”?

Thanks!

1 Like

Time functions and RegEx are some of the most complicated parsing libraries around. Building them in motoko is going to take a significant effort from someone that wants to spend a lot of time doing something that has already been done before.

Your best bet right now is to have your hash maps just be on UTC nat of midnight of the day you want and then build some simple functions that add/multiply the number of days * nanoseconds in a day. You can then parse the UTC on your client using moment.js or something like that to make the requests. Then you pray we don’t have a leap second before someone writes a better library.

2 Likes

I agree. A full-fledged (and correct!) Time.mo is an immense amount of work, and a constant source of grievances. This is why we simply don’t have one yet. If you can, limit the timespan you are interested in, and tabulate seconds from a trustworthy external library, recording mostly the points in time where the result differs from the expected multiply/add result hinted at above. You’ll have just a handful of corrections (e.g. leap seconds) in many years and searching through those is not a big deal.

1 Like

I also have this question right now:(