From 2a14d928a8ae857d671226db5bd4f308425dad18 Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Sun, 21 Oct 2018 12:12:44 +0200 Subject: [PATCH] fix(date): now supports date and time --- readme.md | 3 ++- test/test.date.ts | 5 +++++ ts/smarttime.classes.date.ts | 12 ++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 75f40f5..6c42231 100644 --- a/readme.md +++ b/readme.md @@ -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) diff --git a/test/test.date.ts b/test/test.date.ts index add5905..62ed37e 100644 --- a/test/test.date.ts +++ b/test/test.date.ts @@ -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(); diff --git a/ts/smarttime.classes.date.ts b/ts/smarttime.classes.date.ts index 52c479a..6fbd955 100644 --- a/ts/smarttime.classes.date.ts +++ b/ts/smarttime.classes.date.ts @@ -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); }