summaryrefslogtreecommitdiff
path: root/assets/controllers/time_controller.js
blob: f09bd1aee2e500f29ec314e8d1e13bc39f499197 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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);
  }

}