From 994980a8c77ebe3fbe156b8692679fee3d8d234c Mon Sep 17 00:00:00 2001 From: Thomas <4159106+dtslvr@users.noreply.github.com> Date: Fri, 23 Dec 2022 12:39:49 +0100 Subject: [PATCH] Fix date conversion of two digit year --- apps/client/src/app/adapter/custom-date-adapter.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/apps/client/src/app/adapter/custom-date-adapter.ts b/apps/client/src/app/adapter/custom-date-adapter.ts index af57c567a..663c91b72 100644 --- a/apps/client/src/app/adapter/custom-date-adapter.ts +++ b/apps/client/src/app/adapter/custom-date-adapter.ts @@ -2,7 +2,7 @@ import { Platform } from '@angular/cdk/platform'; import { Inject, forwardRef } from '@angular/core'; import { MAT_DATE_LOCALE, NativeDateAdapter } from '@angular/material/core'; import { getDateFormatString } from '@ghostfolio/common/helper'; -import { format, parse } from 'date-fns'; +import { addYears, format, getYear, parse } from 'date-fns'; export class CustomDateAdapter extends NativeDateAdapter { public constructor( @@ -31,6 +31,16 @@ export class CustomDateAdapter extends NativeDateAdapter { * Parses a date from a provided value */ public parse(aValue: string): Date { - return parse(aValue, getDateFormatString(this.locale), new Date()); + let date = parse(aValue, getDateFormatString(this.locale), new Date()); + + if (getYear(date) < 1900) { + if (getYear(date) > Number(format(new Date(), 'yy')) + 1) { + date = addYears(date, 1900); + } else { + date = addYears(date, 2000); + } + } + + return date; } }