Skip to content

Commit 7d86fe4

Browse files
Andrei CaleniucAndrei Caleniuc
authored andcommitted
fix(dateformat): use luxon for absolute time if enabled
1 parent c9ab137 commit 7d86fe4

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

projects/angular/directives/ui-dateformat/src/ui-dateformat.directive.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import 'moment-timezone';
22

33
import {
4-
DateTime,
5-
DateTimeFormatOptions,
4+
DateTime,
5+
DateTimeFormatOptions,
66
} from 'luxon';
77
import moment from 'moment';
88
import {
@@ -205,15 +205,15 @@ export class UiDateFormatDirective extends UiFormatDirective {
205205
if (!this.date) { return ''; }
206206
if (!(this.date instanceof Date)) { return this.date; }
207207

208-
const absoluteTime = this._isMomentFormat(this.dateFormat)
209-
? moment(this.date)
208+
let absoluteTime;
209+
210+
if (this._useLuxon) {
211+
absoluteTime = this._luxonAbsoluteTime(this.date);
212+
} else if (this._isStringFormat(this.dateFormat)) {
213+
absoluteTime = moment(this.date)
210214
.tz(this.timezone ?? resolveTimezone(this._options))
211-
.format(this.dateFormat)
212-
: DateTime
213-
.fromJSDate(this.date, {
214-
zone: this.timezone ?? resolveTimezone(this._options),
215-
})
216-
.toLocaleString(this.dateFormat);
215+
.format(this.dateFormat);
216+
}
217217

218218
return absoluteTime ?? '';
219219
}
@@ -304,7 +304,7 @@ export class UiDateFormatDirective extends UiFormatDirective {
304304
this._renderer.setAttribute(this._elementRef.nativeElement, 'data-title', this._timeForType(this.titleType));
305305
}
306306

307-
private _isMomentFormat(format: string | DateTimeFormatOptions): format is string {
307+
private _isStringFormat(format: string | DateTimeFormatOptions): format is string {
308308
return typeof format === 'string';
309309
}
310310

@@ -326,4 +326,15 @@ export class UiDateFormatDirective extends UiFormatDirective {
326326

327327
return value !== compareValue;
328328
};
329+
330+
private _luxonAbsoluteTime(date: Date) {
331+
const time = DateTime
332+
.fromJSDate(date, {
333+
zone: this.timezone ?? resolveTimezone(this._options),
334+
});
335+
336+
return this._isStringFormat(this.dateFormat)
337+
? time.toFormat(this.dateFormat)
338+
: time.toLocaleString(this.dateFormat);
339+
}
329340
}

0 commit comments

Comments
 (0)