From 0c6e49aedc107a6586e7c8951389f233302e9cc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Bure=C5=A1?= Date: Tue, 16 Apr 2019 09:37:23 +0200 Subject: [PATCH] Add diff in days --- package.json | 2 +- src/validObjects/ValidDate.test.ts | 16 ++++++++++++++++ src/validObjects/ValidDate.ts | 4 ++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 2de5ff0..53c960d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "valid-objects-ts", - "version": "1.0.5", + "version": "1.0.6", "main": ".dist/index.js", "types": ".dist/index.d.ts", "repository": "git@github.com:Travelport-Czech/valid-objects-ts.git", diff --git a/src/validObjects/ValidDate.test.ts b/src/validObjects/ValidDate.test.ts index a35cd63..89613e7 100644 --- a/src/validObjects/ValidDate.test.ts +++ b/src/validObjects/ValidDate.test.ts @@ -17,4 +17,20 @@ describe('ValidDate', () => { expect(JSON.stringify(data)).to.eq('{"date":"2018-12-24"}') }) + + it('Diff in days', () => { + const date = new ValidDate('2018-12-24') + + const date1 = new ValidDate('2018-12-24') + expect(date.diffInDays(date1)).to.equal(0) + + const date2 = new ValidDate('2018-12-23') + expect(date.diffInDays(date2)).to.equal(1) + + const date3 = new ValidDate('2018-12-25') + expect(date.diffInDays(date3)).to.equal(-1) + + const date4 = new ValidDate('2017-12-24') + expect(date.diffInDays(date4)).to.equal(365) + }) }) diff --git a/src/validObjects/ValidDate.ts b/src/validObjects/ValidDate.ts index 438b4cb..da17c4c 100644 --- a/src/validObjects/ValidDate.ts +++ b/src/validObjects/ValidDate.ts @@ -69,6 +69,10 @@ export class ValidDate { ) } + public readonly diffInDays = (date: ValidDate): number => { + return this.moment().diff(date.moment(), 'days') + } + public readonly formatToSystem = (): string => { return this.moment().format(formatSystemDate) }