connector-wordpress/source/front/date-time-wrapper.js

43 lines
981 B
JavaScript
Raw Normal View History

2021-05-04 11:16:18 +02:00
import { DateTime } from 'luxon'
2021-01-08 14:08:40 +01:00
2021-04-13 20:50:56 +02:00
export default class DateTimeWrapper {
constructor({ locale = 'en-GB', text, timeZone = 'utc' } = {}) {
if (!timeZone) {
timeZone = 'utc'
}
2021-12-09 19:46:19 +01:00
if (
timeZone.includes(':') &&
timeZone.substring(0, 3).toUpperCase() !== 'UTC'
) {
timeZone = 'UTC' + timeZone
}
2021-05-05 10:40:46 +02:00
this.dateTime = DateTime.fromISO(text, { locale, zone: timeZone })
2021-01-08 14:08:40 +01:00
}
getShortDate() {
return this.dateTime.toLocaleString(DateTime.DATE_SHORT)
}
getShortOffsetName() {
return this.dateTime.offsetNameShort
}
2021-01-08 14:08:40 +01:00
get24Time() {
return this.dateTime.toLocaleString(DateTime.TIME_24_SIMPLE)
}
equalsDate(other) {
2021-12-09 19:46:19 +01:00
return (
this.dateTime &&
2021-01-08 14:08:40 +01:00
other.dateTime &&
this.dateTime.day === other.dateTime.day &&
this.dateTime.month === other.dateTime.month &&
this.dateTime.year === other.dateTime.year
2021-12-09 19:46:19 +01:00
)
2021-01-08 14:08:40 +01:00
}
static getCurrentDatetimeAsString() {
2021-05-04 10:37:30 +02:00
return DateTime.now().toString()
}
2021-01-08 14:08:40 +01:00
}