diff options
| author | Georgios Andreadis <G.Andreadis@student.tudelft.nl> | 2017-10-05 22:48:51 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-10-05 22:48:51 +0200 |
| commit | 7b3f7c730c945a392cf8b3bcf472d22be4b2a1cb (patch) | |
| tree | b64633c18714ed693a5ac25b01fd4b6928254c93 /src/util | |
| parent | fe54f3656c2a296e9bcf938dbb8e4be071add72e (diff) | |
| parent | c767083ce47c6a8452dc820c362e46329202f304 (diff) | |
Merge pull request #43 from atlarge-research/fix-timezone-issue
Fix date time locality issue
Diffstat (limited to 'src/util')
| -rw-r--r-- | src/util/date-time.js | 44 | ||||
| -rw-r--r-- | src/util/date-time.test.js | 12 |
2 files changed, 18 insertions, 38 deletions
diff --git a/src/util/date-time.js b/src/util/date-time.js index 2664b8ca..0b752600 100644 --- a/src/util/date-time.js +++ b/src/util/date-time.js @@ -11,35 +11,15 @@ export function parseAndFormatDateTime(dateTimeString) { } /** - * Parses date-time string representations and returns a parsed rack. + * Parses date-time string representations and returns a parsed object. * * The format assumed is "YYYY-MM-DDTHH:MM:SS". * * @param dateTimeString A string expressing a date and a time, in the above mentioned format. - * @returns {object} An rack with the parsed date and time information as content. + * @returns {object} A Date object with the parsed date and time information as content. */ export function parseDateTime(dateTimeString) { - const output = { - year: 0, - month: 0, - day: 0, - hour: 0, - minute: 0, - second: 0 - }; - - const dateAndTime = dateTimeString.split("T"); - const dateComponents = dateAndTime[0].split("-"); - output.year = parseInt(dateComponents[0], 10); - output.month = parseInt(dateComponents[1], 10); - output.day = parseInt(dateComponents[2], 10); - - const timeComponents = dateAndTime[1].split(":"); - output.hour = parseInt(timeComponents[0], 10); - output.minute = parseInt(timeComponents[1], 10); - output.second = parseInt(timeComponents[2], 10); - - return output; + return new Date(dateTimeString + ".000Z"); } /** @@ -53,19 +33,19 @@ export function formatDateTime(dateTime) { const currentDate = new Date(); date = - addPaddingToTwo(dateTime.day) + + addPaddingToTwo(dateTime.getDay()) + "/" + - addPaddingToTwo(dateTime.month) + + addPaddingToTwo(dateTime.getMonth()) + "/" + - addPaddingToTwo(dateTime.year); + addPaddingToTwo(dateTime.getFullYear()); if ( - dateTime.year === currentDate.getFullYear() && - dateTime.month === currentDate.getMonth() + 1 + dateTime.getFullYear() === currentDate.getFullYear() && + dateTime.getMonth() === currentDate.getMonth() ) { - if (dateTime.day === currentDate.getDate()) { + if (dateTime.getDate() === currentDate.getDate()) { date = "Today"; - } else if (dateTime.day === currentDate.getDate() - 1) { + } else if (dateTime.getDate() === currentDate.getDate() - 1) { date = "Yesterday"; } } @@ -73,9 +53,9 @@ export function formatDateTime(dateTime) { return ( date + ", " + - addPaddingToTwo(dateTime.hour) + + addPaddingToTwo(dateTime.getHours()) + ":" + - addPaddingToTwo(dateTime.minute) + addPaddingToTwo(dateTime.getMinutes()) ); } diff --git a/src/util/date-time.test.js b/src/util/date-time.test.js index 958428af..6c7a6b16 100644 --- a/src/util/date-time.test.js +++ b/src/util/date-time.test.js @@ -5,12 +5,12 @@ describe("date-time parsing", () => { const dateString = "2017-09-27T20:55:01"; const parsedDate = parseDateTime(dateString); - expect(parsedDate.year).toEqual(2017); - expect(parsedDate.month).toEqual(9); - expect(parsedDate.day).toEqual(27); - expect(parsedDate.hour).toEqual(20); - expect(parsedDate.minute).toEqual(55); - expect(parsedDate.second).toEqual(1); + expect(parsedDate.getUTCFullYear()).toEqual(2017); + expect(parsedDate.getUTCMonth()).toEqual(8); + expect(parsedDate.getUTCDate()).toEqual(27); + expect(parsedDate.getUTCHours()).toEqual(20); + expect(parsedDate.getUTCMinutes()).toEqual(55); + expect(parsedDate.getUTCSeconds()).toEqual(1); }); }); |
