summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/util/date-time.js44
-rw-r--r--src/util/date-time.test.js12
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);
});
});