Relative Time
Outputs a localized time phrase relative to the current date and time.
Localization is handled by the browser's Intl.RelativeTimeFormat API. No language packs are required.
div( p("Web Awesome 2 release date 🎉"), RelativeTime(_.date := "2020-07-15T09:17:00-04:00")())The date attribute determines when the date/time is calculated from. It must be a string that Date.parse() can interpret or a Date object set via JavaScript.
When using strings, avoid ambiguous dates such as 03/04/2020 which can be interpreted as March 4 or April 3 depending on the user's browser and locale. Instead, always use a valid ISO 8601 date time string to ensure the date will be parsed properly by all clients.
Examples
Keeping Time in Sync
Use the sync attribute to update the displayed value automatically as time passes.
RelativeTime( _.sync := true, _.date := { // Set the date to 1 minute ago val oneMinuteAgo = new js.Date(js.Date.now() - 60000) oneMinuteAgo.toISOString() })()Formatting Styles
You can change how the time is displayed using the format attribute. Note that some locales may display the same values for narrow and short formats.
div("Narrow: ", RelativeTime(_.date := "2020-07-15T09:17:00-04:00", _.format.narrow)())div("Short: ", RelativeTime(_.date := "2020-07-15T09:17:00-04:00", _.format.short)())div("Long: ", RelativeTime(_.date := "2020-07-15T09:17:00-04:00", _.format.long)())Localization
Use the lang attribute to set the desired locale.
div("English: ", RelativeTime(_.date := "2020-07-15T09:17:00-04:00")(lang := "en-US"))div("Chinese: ", RelativeTime(_.date := "2020-07-15T09:17:00-04:00")(lang := "zh-CN"))div("German: ", RelativeTime(_.date := "2020-07-15T09:17:00-04:00")(lang := "de"))div("Greek: ", RelativeTime(_.date := "2020-07-15T09:17:00-04:00")(lang := "el"))div("Russian: ", RelativeTime(_.date := "2020-07-15T09:17:00-04:00")(lang := "ru"))