Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing DateTimeFormatter.ofLocalizedDateTime #8

Open
OndrejSpanel opened this issue Sep 30, 2019 · 1 comment
Open

Missing DateTimeFormatter.ofLocalizedDateTime #8

OndrejSpanel opened this issue Sep 30, 2019 · 1 comment

Comments

@OndrejSpanel
Copy link

OndrejSpanel commented Sep 30, 2019

When porting some JVM code to JS I was hit by a method DateTimeFormatter.ofLocalizedDateTime not implemented. Would it be possible to add it?

What alternatives does one have without it to get locale dependent long or short time formats?

@OndrejSpanel
Copy link
Author

OndrejSpanel commented Sep 30, 2019

I understand now this is a known limitation:

No Locale support.

My current workaround is using js.Date and Intl functionality:

  implicit class ZonedDateTimeOps(t: ZonedDateTime) {
    def toJSDate: js.Date = {
      val text = t.toString // (DateTimeFormatter.ISO_ZONED_DATE_TIME)
      new js.Date(js.Date.parse(text))
    }
  }

  def locale: String = {
    import org.scalajs.dom
    val firstLanguage = dom.window.navigator.asInstanceOf[js.Dynamic].languages.asInstanceOf[js.Array[String]].headOption
    firstLanguage.getOrElse(dom.window.navigator.language)
  }

  def formatDateTime(t: js.Date): String = {
    new intl.DateTimeFormat(
      locale,
      intl.DateTimeFormatOptions(
        year = "numeric",
        month = "numeric",
        day = "numeric",
        hour = "numeric",
        minute = "numeric"
      )
    ).format(t)
  }
  }

Used as:

  val x: ZonedDateTime = ???
  formatDateTime(x.toJSDate)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant