2023-06-27 18:03:52 +00:00
|
|
|
import { Controller } from "@hotwired/stimulus";
|
|
|
|
|
|
|
|
// Replaces all UTC timestamps with time formated for the local timezone
|
|
|
|
export default class extends Controller {
|
|
|
|
connect() {
|
|
|
|
this.renderLocalTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
renderLocalTime() {
|
|
|
|
this.element.textContent = this.localTimeString;
|
|
|
|
}
|
|
|
|
|
|
|
|
get localTimeString(): string {
|
|
|
|
if (this.utcTime) {
|
2023-06-29 04:34:09 +00:00
|
|
|
return this.utcTime.toLocaleDateString(window.navigator.language, {
|
|
|
|
year: "numeric",
|
|
|
|
month: "long",
|
|
|
|
day: "numeric",
|
|
|
|
});
|
2023-06-27 18:03:52 +00:00
|
|
|
}
|
|
|
|
return "Unknown datetime"
|
|
|
|
}
|
|
|
|
|
|
|
|
get utcTime(): Date | null {
|
|
|
|
const utcString = this.element.getAttribute("datetime");
|
|
|
|
return utcString ? new Date(utcString) : null;
|
|
|
|
}
|
|
|
|
}
|