fix(date): now supports date and time

This commit is contained in:
Philipp Kunz 2018-10-21 12:12:44 +02:00
parent f161f71c4e
commit 2a14d928a8
3 changed files with 19 additions and 1 deletions

View File

@ -19,12 +19,13 @@ handle timeformats in smart ways
[![bitHound Code](https://www.bithound.io/github/pushrocks/smarttime/badges/code.svg)](https://www.bithound.io/github/pushrocks/smarttime)
[![TypeScript](https://img.shields.io/badge/TypeScript-2.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
[![node](https://img.shields.io/badge/node->=%206.x.x-blue.svg)](https://nodejs.org/dist/latest-v6.x/docs/api/)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)
## Usage
Use TypeScript for best in class instellisense.
Smarttime offers smart ways to deal with time.
For further information read the linked docs at the top of this README.
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)

View File

@ -7,4 +7,9 @@ tap.test('should create a valid JavaScript Date from European TimeStamp', async
console.log(extendedDate);
});
tap.test('should create a date and time with European Format', async () => {
const extendedDate = smarttime.ExtendedDate.fromEuropeanDateAndTime('10.10.2018', '08:00:00', 'Europe/Berlin');
console.log(extendedDate);
})
tap.start();

View File

@ -1,5 +1,7 @@
import * as plugins from './smarttime.plugins';
export type TAvailableZone = 'Europe/Berlin';
export class ExtendedDate extends Date {
public static fromEuropeanDate(europeanDate: string) {
const dateArray = /(.*)\.(.*)\.(.*)/.exec(europeanDate);
@ -12,6 +14,16 @@ export class ExtendedDate extends Date {
return new ExtendedDate(unixMilli);
}
/** */
public static fromEuropeanDateAndTime(europeanDateArg: string, timeArg: string, zoneArg: TAvailableZone) {
const dateArray = /(.*)\.(.*)\.(.*)/.exec(europeanDateArg);
const luxonDate = plugins.luxon.DateTime.fromISO(`${dateArray[3]}-${dateArray[2]}-${dateArray[1]}T${timeArg}`, {
zone: zoneArg
});
const unixMilli = luxonDate.toMillis();
return new ExtendedDate(unixMilli);
}
constructor(unixMilli: number) {
super(unixMilli);
}