From beb6930e2b2762a4dd076bbd1c87dff8e74e03d9 Mon Sep 17 00:00:00 2001 From: Sujit Singh Date: Thu, 24 Dec 2020 19:42:30 +0530 Subject: [PATCH 1/3] Added snippet for getDifferenceInSeconds --- snippets/covertArrayTo2DMatrix.md | 33 ++++++++++++++++++++++++++++++ snippets/getDifferenceInSeconds.md | 15 ++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 snippets/covertArrayTo2DMatrix.md create mode 100644 snippets/getDifferenceInSeconds.md diff --git a/snippets/covertArrayTo2DMatrix.md b/snippets/covertArrayTo2DMatrix.md new file mode 100644 index 000000000..0ab962d3a --- /dev/null +++ b/snippets/covertArrayTo2DMatrix.md @@ -0,0 +1,33 @@ +--- +title: covertArrayTo2DMatrix +tags: array,intermediate +--- + +Returns array of arrays, with internal arrays of size (columns) supplied. + +```js +const covertArrayTo2DMatrix = (items, columns) => { + let data = [] + if (items?.length && columns) { + const rows = columns ? Math.ceil((items?.length ?? 0) / columns) : 0 + for (let i = 0; i < rows; i++) { + let row = [] + for (let j = 0; j < columns; j++) { + const element = items[i + j + i * (columns - 1)] + if (element) { + row.push(element) + } + } + + data.push(row) + } + } + return data +} +``` + +```js +covertArrayTo2DMatrix([1, 2, 3, 4, 5, 6], 3); // [[1, 2, 3], [4, 5, 6]] +covertArrayTo2DMatrix([1, 2, 3, 4, 5, 6], 2); // [[1, 2], [3, 4], [5, 6]] +covertArrayTo2DMatrix([1, 2, 3, 4, 5], 3); // [[1, 2, 3], [4, 5]] +``` diff --git a/snippets/getDifferenceInSeconds.md b/snippets/getDifferenceInSeconds.md new file mode 100644 index 000000000..ea30aad74 --- /dev/null +++ b/snippets/getDifferenceInSeconds.md @@ -0,0 +1,15 @@ +--- +title: getDifferenceInSeconds +tags: dates,beginner +--- + +Returns the diffrence in seconds between 2 supplied dates. + +```js +const getDifferenceInSeconds = (start, end) => + (end.getTime() - start.getTime()) / 1000; +``` + +```js +getDifferenceInSeconds(new Date('2020-12-24 00:00:15'), new Date('2020-12-24 00:00:17')); // 2 +``` From a7309e5754deb0cbe2a6c54ba535cd87831645c5 Mon Sep 17 00:00:00 2001 From: Sujit Singh Date: Thu, 24 Dec 2020 19:45:14 +0530 Subject: [PATCH 2/3] Removed covertArrayTo2DMatrix --- snippets/covertArrayTo2DMatrix.md | 33 ------------------------------- 1 file changed, 33 deletions(-) delete mode 100644 snippets/covertArrayTo2DMatrix.md diff --git a/snippets/covertArrayTo2DMatrix.md b/snippets/covertArrayTo2DMatrix.md deleted file mode 100644 index 0ab962d3a..000000000 --- a/snippets/covertArrayTo2DMatrix.md +++ /dev/null @@ -1,33 +0,0 @@ ---- -title: covertArrayTo2DMatrix -tags: array,intermediate ---- - -Returns array of arrays, with internal arrays of size (columns) supplied. - -```js -const covertArrayTo2DMatrix = (items, columns) => { - let data = [] - if (items?.length && columns) { - const rows = columns ? Math.ceil((items?.length ?? 0) / columns) : 0 - for (let i = 0; i < rows; i++) { - let row = [] - for (let j = 0; j < columns; j++) { - const element = items[i + j + i * (columns - 1)] - if (element) { - row.push(element) - } - } - - data.push(row) - } - } - return data -} -``` - -```js -covertArrayTo2DMatrix([1, 2, 3, 4, 5, 6], 3); // [[1, 2, 3], [4, 5, 6]] -covertArrayTo2DMatrix([1, 2, 3, 4, 5, 6], 2); // [[1, 2], [3, 4], [5, 6]] -covertArrayTo2DMatrix([1, 2, 3, 4, 5], 3); // [[1, 2, 3], [4, 5]] -``` From 65bfb333eb66956a82573c0f818e87e91e9d3ba0 Mon Sep 17 00:00:00 2001 From: Isabelle Viktoria Maciohsek Date: Sat, 24 Apr 2021 12:39:48 +0300 Subject: [PATCH 3/3] Update and rename getDifferenceInSeconds.md to getSecondsDiffBetweenDates.md --- snippets/getDifferenceInSeconds.md | 15 --------------- snippets/getSecondsDiffBetweenDates.md | 20 ++++++++++++++++++++ 2 files changed, 20 insertions(+), 15 deletions(-) delete mode 100644 snippets/getDifferenceInSeconds.md create mode 100644 snippets/getSecondsDiffBetweenDates.md diff --git a/snippets/getDifferenceInSeconds.md b/snippets/getDifferenceInSeconds.md deleted file mode 100644 index ea30aad74..000000000 --- a/snippets/getDifferenceInSeconds.md +++ /dev/null @@ -1,15 +0,0 @@ ---- -title: getDifferenceInSeconds -tags: dates,beginner ---- - -Returns the diffrence in seconds between 2 supplied dates. - -```js -const getDifferenceInSeconds = (start, end) => - (end.getTime() - start.getTime()) / 1000; -``` - -```js -getDifferenceInSeconds(new Date('2020-12-24 00:00:15'), new Date('2020-12-24 00:00:17')); // 2 -``` diff --git a/snippets/getSecondsDiffBetweenDates.md b/snippets/getSecondsDiffBetweenDates.md new file mode 100644 index 000000000..91e2bf16d --- /dev/null +++ b/snippets/getSecondsDiffBetweenDates.md @@ -0,0 +1,20 @@ +--- +title: getSecondsDiffBetweenDates +tags: date,beginner +--- + +Calculates the difference (in seconds) between two dates. + +- Subtract the two `Date` objects and divide by the number of milliseconds in a second to get the difference (in seconds) between them. + +```js +const getSecondsDiffBetweenDates = (dateInitial, dateFinal) => + (dateFinal - dateInitial) / 1000; +``` + +```js +getSecondsDiffBetweenDates( + new Date('2020-12-24 00:00:15'), + new Date('2020-12-24 00:00:17') +); // 2 +```