Sha256: 1659b8fd756f75c4caf17742338e034dc9a14b5b71a8d9e5cd94053b9f3825c6

Contents?: true

Size: 902 Bytes

Versions: 35

Compression:

Stored size: 902 Bytes

Contents

import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  static targets = [ "time", "date", "datetime" ]

  initialize() {
    this.timeFormatter = new Intl.DateTimeFormat(undefined, { timeStyle: "short" })
    this.dateFormatter = new Intl.DateTimeFormat(undefined, { dateStyle: "long" })
    this.dateTimeFormatter = new Intl.DateTimeFormat(undefined, { timeStyle: "short", dateStyle: "short" })
  }

  timeTargetConnected(target) {
    this.#formatTime(this.timeFormatter, target)
  }

  dateTargetConnected(target) {
    this.#formatTime(this.dateFormatter, target)
  }

  datetimeTargetConnected(target) {
    this.#formatTime(this.dateTimeFormatter, target)
  }

  #formatTime(formatter, target) {
    const dt = new Date(target.getAttribute("datetime"))
    target.textContent = formatter.format(dt)
    target.title = this.dateTimeFormatter.format(dt)
  }
}

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
css-zero-0.0.72 lib/generators/css_zero/add/templates/app/javascript/controllers/local_time_controller.js
css-zero-0.0.71 lib/generators/css_zero/add/templates/app/javascript/controllers/local_time_controller.js
css-zero-0.0.70 lib/generators/css_zero/add/templates/app/javascript/controllers/local_time_controller.js
css-zero-0.0.69 lib/generators/css_zero/add/templates/app/javascript/controllers/local_time_controller.js
css-zero-0.0.68 lib/generators/css_zero/add/templates/app/javascript/controllers/local_time_controller.js
css-zero-0.0.67 lib/generators/css_zero/add/templates/app/javascript/controllers/local_time_controller.js
css-zero-0.0.66 lib/generators/css_zero/add/templates/app/javascript/controllers/local_time_controller.js
css-zero-0.0.65 lib/generators/css_zero/add/templates/app/javascript/controllers/local_time_controller.js
css-zero-0.0.64 lib/generators/css_zero/add/templates/app/javascript/controllers/local_time_controller.js
css-zero-0.0.63 lib/generators/css_zero/add/templates/app/javascript/controllers/local_time_controller.js
css-zero-0.0.62 lib/generators/css_zero/add/templates/app/javascript/controllers/local_time_controller.js
css-zero-0.0.61 lib/generators/css_zero/add/templates/app/javascript/controllers/local_time_controller.js
css-zero-0.0.60 lib/generators/css_zero/add/templates/app/javascript/controllers/local_time_controller.js
css-zero-0.0.59 lib/generators/css_zero/add/templates/app/javascript/controllers/local_time_controller.js
css-zero-0.0.58 lib/generators/css_zero/add/templates/app/javascript/controllers/local_time_controller.js
css-zero-0.0.57 lib/generators/css_zero/add/templates/app/javascript/controllers/local_time_controller.js
css-zero-0.0.56 lib/generators/css_zero/add/templates/app/javascript/controllers/local_time_controller.js
css-zero-0.0.55 lib/generators/css_zero/add/templates/app/javascript/controllers/local_time_controller.js
css-zero-0.0.54 lib/generators/css_zero/add/templates/app/javascript/controllers/local_time_controller.js
css-zero-0.0.53 lib/generators/css_zero/add/templates/app/javascript/controllers/local_time_controller.js