diff options
Diffstat (limited to 'assets/controllers')
-rw-r--r-- | assets/controllers/time_controller.js | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/assets/controllers/time_controller.js b/assets/controllers/time_controller.js new file mode 100644 index 0000000..f09bd1a --- /dev/null +++ b/assets/controllers/time_controller.js @@ -0,0 +1,19 @@ +import { Controller } from "stimulus"; +import { DateTime } from "luxon"; + +export default class extends Controller { + static values = { format: String }; + + connect() { + var browserTz = Intl.DateTimeFormat().resolvedOptions().timeZone; + var browserLocale = Intl.DateTimeFormat().resolvedOptions().locale; + var locale = document.body.dataset.locale || browserLocale || "en"; + var tz = document.body.dataset.tz || browserTz || "UTC"; + var style = DateTime[this.formatValue]; + console.log(locale,tz,style); + this.date = DateTime.fromISO(this.element.dateTime).setZone(tz).setLocale(locale); + this.element.innerText = this.date.toLocaleString(style); + } + +} + |