fix(core): update

This commit is contained in:
Philipp Kunz 2019-06-18 14:38:57 +02:00
parent 388649659c
commit 30d18db4af
2 changed files with 82 additions and 1 deletions

View File

@ -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();

View File

@ -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()]
};
}
}