scaladatescala.js

How to format / parse a Date in Scala and ScalaJS


I want to use Dates in shared code (JVM and JS).

So I need the functionality to parse a Date from a Text and to format a Text from a Date.

I found that ScalaJS supports java.util.Date and java.util.Formatter, but with this there is no way to parse a Text to a Date.

Is it possible to have this in shared code? Or what is the best practice to handle dates in this scenario.


Solution

  • You can use the java.time APIs through https://github.com/cquiroz/scala-java-time to manipulate date/time in a cross-platform way.

    For parsing, use the java.text APIs through https://github.com/cquiroz/scala-java-locales.