demarches-normaliennes/app/javascript/controllers/datetime_controller.ts

26 lines
694 B
TypeScript
Raw Normal View History

2024-07-05 14:34:53 +02:00
import { format } from 'date-fns/format';
import { ApplicationController } from './application_controller';
export class DatetimeController extends ApplicationController {
#isSupported = isDateTimeSupported();
connect() {
if (!this.#isSupported) {
const value = this.element.getAttribute('value');
if (value) {
const date = new Date(value);
this.element.setAttribute('value', format(date, `dd/MM/yyyy HH:mm`));
}
}
}
}
function isDateTimeSupported() {
const input = document.createElement('input');
const value = 'a';
input.setAttribute('type', 'datetime-local');
input.setAttribute('value', value);
return input.value !== value;
}