Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/app/core/utils/dates.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { TestBed } from '@angular/core/testing';
import { DatePipe } from '@angular/common';
import { DatePipe, registerLocaleData } from '@angular/common';
import localeEN from '@angular/common/locales/en';
import { Dates } from './dates';

registerLocaleData(localeEN);

describe('Dates', () => {
let service: Dates;

Expand Down Expand Up @@ -99,4 +102,20 @@ describe('Dates', () => {
});
});
});

describe('formatDate', () => {
it('should fall back to moment formatting for a valid date when locale is unregistered', () => {
localStorage.setItem('mifosXLanguage', JSON.stringify({ name: 'xx', code: 'xx' }));
const date = new Date(2025, 0, 15);
const result = service.formatDate(date, 'yyyy-MM-dd');
expect(result).toBe('2025-01-15');
localStorage.removeItem('mifosXLanguage');
});

it('should throw for an invalid timestamp instead of returning "Invalid date"', () => {
localStorage.setItem('mifosXLanguage', JSON.stringify({ name: 'xx', code: 'xx' }));
expect(() => service.formatDate('not-a-date', 'yyyy-MM-dd')).toThrow();
localStorage.removeItem('mifosXLanguage');
});
});
});
11 changes: 9 additions & 2 deletions src/app/core/utils/dates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,15 @@ export class Dates {
}

public formatDate(timestamp: any, dateFormat: string): string {
const datePipe: DatePipe = new DatePipe(this.language.code);
return datePipe.transform(timestamp, dateFormat);
try {
const datePipe: DatePipe = new DatePipe(this.language.code);
return datePipe.transform(timestamp, dateFormat);
} catch (e) {
if (moment(timestamp).isValid()) {
return this.formatDateAsString(timestamp, dateFormat);
}
throw e;
Comment thread
barbierajput378-pixel marked this conversation as resolved.
}
}

public formatDateAsString(value: Date, dateFormat: string): string {
Expand Down
1 change: 1 addition & 0 deletions src/app/pipes/date-format.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Subscription } from 'rxjs';
import moment from 'moment';

// Load moment.js locale data for all supported languages
import 'moment/locale/az'; // Azerbaijani
import 'moment/locale/ne'; // Nepali
import 'moment/locale/es'; // Spanish
import 'moment/locale/de'; // German
Expand Down
2 changes: 2 additions & 0 deletions src/app/web-app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import { ThemingService } from './shared/theme-toggle/theming.service';
const log = new Logger('MifosX');

import { registerLocaleData } from '@angular/common';
import localeAZ from '@angular/common/locales/az';
import localeCS from '@angular/common/locales/cs';
import localeEN from '@angular/common/locales/en';
import localeES from '@angular/common/locales/es';
Expand All @@ -60,6 +61,7 @@ import localePT from '@angular/common/locales/pt';
import localeSW from '@angular/common/locales/sw';
import { STANDALONE_SHARED_IMPORTS } from 'app/standalone-shared.module';

registerLocaleData(localeAZ);
registerLocaleData(localeCS);
registerLocaleData(localeEN);
registerLocaleData(localeES);
Expand Down
Loading