diff --git a/src/date.test.ts b/src/date.test.ts index bca37f3..5bebe5e 100644 --- a/src/date.test.ts +++ b/src/date.test.ts @@ -6,6 +6,7 @@ import { formatGermanFullDateTime, isFuture, isToday, + isValidGermanDate, parseGermanDate, parseGermanDateFlexible, parseIsoDate, @@ -63,6 +64,35 @@ describe('formatGermanDateTime', () => { }); }); +describe('isValidGermanDate', () => { + it('should determine if a string matches the german date format dd.mm.yyyy', () => { + expect(isValidGermanDate('01.01.2000')).toEqual(true); + expect(isValidGermanDate('1.1.2000')).toEqual(true); + expect(isValidGermanDate('1.01.2000')).toEqual(true); + expect(isValidGermanDate('01.1.2000')).toEqual(true); + expect(isValidGermanDate('011.1.2000')).toEqual(false); + expect(isValidGermanDate('01.111.2000')).toEqual(false); + expect(isValidGermanDate('01.01.200')).toEqual(false); + expect(isValidGermanDate('xx.01.2000')).toEqual(false); + expect(isValidGermanDate('01.yy.2000')).toEqual(false); + expect(isValidGermanDate('01.01.zzzz')).toEqual(false); + expect(isValidGermanDate('')).toEqual(false); + expect(isValidGermanDate('..')).toEqual(false); + expect(isValidGermanDate('abc')).toEqual(false); + }); +}); + +describe('isValidTime', () => { + it('should determine if a string matches the time format HH:mm', () => { + expect(isValidGermanDate('12:55')).toEqual(true); + expect(isValidGermanDate('1:55')).toEqual(false); + expect(isValidGermanDate('12:5')).toEqual(false); + expect(isValidGermanDate('')).toEqual(false); + expect(isValidGermanDate(':')).toEqual(false); + expect(isValidGermanDate('abc')).toEqual(false); + }); +}); + describe('isToday', () => { it('should determine if a date is today', () => { expect(isToday(new Date())).toEqual(true); diff --git a/src/date.ts b/src/date.ts index e18d28b..97a68bd 100644 --- a/src/date.ts +++ b/src/date.ts @@ -15,6 +15,27 @@ export function isFuture(date: Date | string | number): boolean { return isAfter(date, new Date()); } +export function isValidGermanDate(date: string): boolean { + const regexGermanDateFormat = /^(\d{1,2}[.]\d{1,2}[.]\d{4})$/; + + if (date.match(regexGermanDateFormat)) { + return true; + } + + return false; +} + +export function isValidTime(time: string): boolean { + const regexTimeFormat = /^(\d{2}[:]\d{2})$/; + + if (time.match(regexTimeFormat)) { + return true; + } + + return false; +} + + export function parseDate(value: string, formatString: string): Date | null { const parsedDate = parse(value, formatString, new Date());