summaryrefslogblamecommitdiff
path: root/assets/controllers/time_controller.js
blob: f09bd1aee2e500f29ec314e8d1e13bc39f499197 (plain) (tree)


















                                                                                      
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);
  }

}