WEB-693: Implement Date and Datetime Global Variables (#3123)

Signed-off-by: DeathGun44 <krishnamewara841@gmail.com>
This commit is contained in:
Krishna Mewara
2026-02-15 15:35:42 -06:00
committed by GitHub
parent f60057ae0a
commit 2317e5009c
25 changed files with 290 additions and 50 deletions
+4 -14
View File
@@ -9,6 +9,7 @@
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 from 'moment';
@@ -29,6 +30,7 @@ import 'moment/locale/cs'; // Czech
export class DateFormatPipe implements PipeTransform, OnDestroy {
private settingsService = inject(SettingsService);
private translateService = inject(TranslateService);
private dateUtils = inject(Dates);
private onLangChange: Subscription;
constructor() {
@@ -45,24 +47,12 @@ export class DateFormatPipe implements PipeTransform, OnDestroy {
}
transform(value: any, dateFormat?: string): any {
const defaultDateFormat = this.settingsService.dateFormat.replace('dd', 'DD');
const defaultDateFormat = this.dateUtils.angularToMomentFormat(this.settingsService.dateFormat);
if (typeof value === 'undefined' || value === null) {
return '';
}
// Map SettingsService language code to a moment.js locale identifier
const langCode = this.settingsService.language.code;
let momentLocale: string;
if (!langCode) {
momentLocale = 'en';
} else if (langCode.includes('-')) {
// Convert codes like 'ne-NE' or 'es-MX' to base language 'ne', 'es'
momentLocale = langCode.split('-')[0];
} else {
momentLocale = langCode;
}
const momentLocale = this.dateUtils.getMomentLocale(this.settingsService.language);
moment.locale(momentLocale);
let dateVal;
+46 -7
View File
@@ -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);
}
}