WEB-693: Implement Date and Datetime Global Variables (#3123)
Signed-off-by: DeathGun44 <krishnamewara841@gmail.com>
This commit is contained in:
@@ -6,13 +6,38 @@
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
import { Pipe, PipeTransform } from '@angular/core';
|
||||
import { Pipe, PipeTransform, inject, OnDestroy } from '@angular/core';
|
||||
import { SettingsService } from 'app/settings/settings.service';
|
||||
import { TranslateService } from '@ngx-translate/core';
|
||||
import { Dates } from 'app/core/utils/dates';
|
||||
import { Subscription } from 'rxjs';
|
||||
import moment, { Moment } from 'moment';
|
||||
|
||||
@Pipe({ name: 'datetimeFormat', standalone: true })
|
||||
export class DatetimeFormatPipe implements PipeTransform {
|
||||
@Pipe({ name: 'datetimeFormat', pure: false })
|
||||
export class DatetimeFormatPipe implements PipeTransform, OnDestroy {
|
||||
private settingsService = inject(SettingsService);
|
||||
private translateService = inject(TranslateService);
|
||||
private dateUtils = inject(Dates);
|
||||
private onLangChange: Subscription;
|
||||
|
||||
constructor() {
|
||||
this.onLangChange = this.translateService.onLangChange.subscribe(() => {});
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
if (this.onLangChange) {
|
||||
this.onLangChange.unsubscribe();
|
||||
}
|
||||
}
|
||||
|
||||
transform(value: unknown, datetimeFormat?: string): string {
|
||||
const defaultDatetimeFormat = this.dateUtils.angularToMomentFormat(this.settingsService.datetimeFormat);
|
||||
|
||||
if (value == null || value === '') return '';
|
||||
|
||||
const momentLocale = this.dateUtils.getMomentLocale(this.settingsService.language);
|
||||
moment.locale(momentLocale);
|
||||
|
||||
let dateVal: Moment;
|
||||
if (Array.isArray(value)) {
|
||||
const [
|
||||
@@ -24,17 +49,31 @@ export class DatetimeFormatPipe implements PipeTransform {
|
||||
ss
|
||||
] = value as number[];
|
||||
if (hh != null) {
|
||||
dateVal = moment({ year: y, month: (m ?? 1) - 1, date: d, hour: hh, minute: mm ?? 0, second: ss ?? 0 });
|
||||
dateVal = moment({
|
||||
year: y,
|
||||
month: (m ?? 1) - 1,
|
||||
date: d,
|
||||
hour: hh,
|
||||
minute: mm ?? 0,
|
||||
second: ss ?? 0
|
||||
});
|
||||
} else {
|
||||
dateVal = moment({ year: y, month: (m ?? 1) - 1, date: d });
|
||||
}
|
||||
} else if (typeof value === 'number' && value < 1e12) {
|
||||
// epoch seconds
|
||||
dateVal = moment.unix(value);
|
||||
} else {
|
||||
dateVal = moment(value as any);
|
||||
}
|
||||
const fmt = datetimeFormat ?? 'YYYY-MM-DDTHH:mm:ssZ';
|
||||
return dateVal.format(fmt);
|
||||
|
||||
if (!dateVal.isValid()) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (!datetimeFormat) {
|
||||
return dateVal.format(defaultDatetimeFormat);
|
||||
}
|
||||
|
||||
return dateVal.format(datetimeFormat);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user