From 30d18db4afdab9bda197324da3520748bdaa39ae Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Tue, 18 Jun 2019 14:38:57 +0200 Subject: [PATCH] fix(core): update --- test/test.extendeddate.ts | 5 ++ ts/smarttime.classes.extendeddate.ts | 78 +++++++++++++++++++++++++++- 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/test/test.extendeddate.ts b/test/test.extendeddate.ts index 5e90f43..ebd4407 100644 --- a/test/test.extendeddate.ts +++ b/test/test.extendeddate.ts @@ -16,4 +16,9 @@ tap.test('should create a date and time with European Format', async () => { console.log(extendedDate); }); +tap.test('should create a European date string', async () => { + const extendedDate = smarttime.ExtendedDate.fromHyphedDate('2018-02-13'); + expect(extendedDate.exportToEuropeanDate()).to.equal('13.02.2018'); +}); + tap.start(); diff --git a/ts/smarttime.classes.extendeddate.ts b/ts/smarttime.classes.extendeddate.ts index ba765f7..d5176d9 100644 --- a/ts/smarttime.classes.extendeddate.ts +++ b/ts/smarttime.classes.extendeddate.ts @@ -2,7 +2,20 @@ import * as plugins from './smarttime.plugins'; export type TAvailableZone = 'Europe/Berlin'; +export interface IDateUnits { + year: number; + yearString: string; + month: number; + monthString: string; + monthName: string; + day: number; + dayString: string; + dayOfTheWeek: number; + dayOfTheWeekName: string; +} + export class ExtendedDate extends Date { + // STATIC factories public static fromMillis(milliSeconds: number) { return new ExtendedDate(milliSeconds); } @@ -18,12 +31,28 @@ export class ExtendedDate extends Date { return new ExtendedDate(unixMilli); } - /** */ + /** + * creates an Extended date from a hypedDate like "2018-03-28" + * @param dateString + */ + public static fromHyphedDate(dateString: string) { + // guards + // implementation + const dateMillis = new Date(dateString).getTime(); + return new ExtendedDate(dateMillis); + } + + /** + * Same as .fromEuropeanDate(), but accepts additional timeArg and zoneArg + */ public static fromEuropeanDateAndTime( europeanDateArg: string, timeArg: string = '12:00:00', zoneArg: TAvailableZone = 'Europe/Berlin' ) { + // guards + + // implementation const dateArray = /(.*)\.(.*)\.(.*)/.exec(europeanDateArg); const sliceDate = (dateString: string) => { return `0${dateString}`.slice(-2); @@ -38,7 +67,54 @@ export class ExtendedDate extends Date { return new ExtendedDate(unixMilli); } + // INSTANCE constructor(unixMilli: number) { super(unixMilli); } + + // + public exportToEuropeanDate() { + const units = this.exportToUnits(); + return `${units.dayString}.${units.monthString}.${units.yearString}`; + } + + /** + * exports units + */ + public exportToUnits(): IDateUnits { + const monthsArray = [ + 'January', + 'February', + 'March', + 'April', + 'May', + 'June', + 'July', + 'August', + 'September', + 'October', + 'November', + 'December' + ]; + const daysArray = [ + 'Monday', + 'Tuesday', + 'Wednesday', + 'Thursday', + 'Friday', + 'Saturday', + 'Sunday' + ]; + return { + year: this.getFullYear(), + yearString: `${this.getFullYear()}`, + month: this.getMonth() + 1, + monthString: ("0" + (this.getMonth() + 1)).slice(-2), + monthName: monthsArray[this.getMonth()], + day: this.getDate(), + dayString: ("0" + this.getDate()).slice(-2), + dayOfTheWeek: this.getDay(), + dayOfTheWeekName: daysArray[this.getDay()] + }; + } }