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 {
|
2021-05-05 11:36:57 +02:00
|
|
|
constructor({ locale = 'en-GB', text, timeZone = 'utc' } = {}) {
|
2021-08-24 19:23:57 +02:00
|
|
|
if (!timeZone) {
|
|
|
|
timeZone = 'utc'
|
|
|
|
}
|
2021-12-09 19:46:19 +01:00
|
|
|
if (
|
|
|
|
timeZone.includes(':') &&
|
|
|
|
timeZone.substring(0, 3).toUpperCase() !== 'UTC'
|
|
|
|
) {
|
2021-08-24 19:23:57 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-05-23 15:37:29 +02:00
|
|
|
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
|
|
|
}
|
2021-01-15 20:30:05 +01:00
|
|
|
|
|
|
|
static getCurrentDatetimeAsString() {
|
2021-05-04 10:37:30 +02:00
|
|
|
return DateTime.now().toString()
|
|
|
|
}
|
2021-01-08 14:08:40 +01:00
|
|
|
}
|