diff --git a/README.md b/README.md index 44bf25bb4..cc5c01a4f 100644 --- a/README.md +++ b/README.md @@ -212,7 +212,9 @@ average(1, 2, 3); View contents * [`formatDuration`](#formatduration) +* [`getColonTimeFromDate`](#getcolontimefromdate) * [`getDaysDiffBetweenDates`](#getdaysdiffbetweendates) +* [`getMeridiemSuffixOfInteger`](#getmeridiemsuffixofinteger) * [`tomorrow`](#tomorrow) @@ -430,16 +432,6 @@ average(1, 2, 3); -### _Uncategorized_ - -
-View contents - -* [`getColonTimeFromDate`](#getcolontimefromdate) -* [`getMeridiemSuffixOfInteger`](#getmeridiemsuffixofinteger) - -
- --- ## 🔌 Adapter @@ -3488,6 +3480,28 @@ formatDuration(34325055574); // '397 days, 6 hours, 44 minutes, 15 seconds, 574
[⬆ Back to top](#table-of-contents) +### getColonTimeFromDate + +Returns a string of the form `HH:MM:SS` from a `Date` object. + +Use `Date.toString()` and `String.slice()` to get the `HH:MM:SS` part of a given `Date` object. + +```js +const getColonTimeFromDate = date => date.toTimeString().slice(0, 8); +``` + +
+Examples + +```js +getColonTimeFromDate(new Date()); // "08:38:00" +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### getDaysDiffBetweenDates Returns the difference (in days) between two dates. @@ -3511,6 +3525,34 @@ getDaysDiffBetweenDates(new Date('2017-12-13'), new Date('2017-12-22')); // 9
[⬆ Back to top](#table-of-contents) +### getMeridiemSuffixOfInteger + +Converts an integer to a suffixed string, adding `am` or `pm` based on its value. + +Use the modulo operator (`%`) and conditional checks to transform an integer to a stringified 12-hour format with meridiem suffix. + +```js +const getMeridiemSuffixOfInteger = num => + num === 0 || num === 24 + ? 12 + 'am' + : num === 12 ? 12 + 'pm' : num < 12 ? num % 12 + 'am' : num % 12 + 'pm'; +``` + +
+Examples + +```js +getMeridiemSuffixOfInteger(0); // "12am" +getMeridiemSuffixOfInteger(11); // "11am" +getMeridiemSuffixOfInteger(13); // "1pm" +getMeridiemSuffixOfInteger(25); // "1pm" +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### tomorrow Results in a string representation of tomorrow's date. @@ -7898,48 +7940,6 @@ yesNo('Foo', true); // true
[⬆ Back to top](#table-of-contents) ---- - ## _Uncategorized_ - -### getColonTimeFromDate - -Returns a string of the form `HH:MM:SS` from a `Date` object. - -Use `Date.toString()` and `String.slice()` to get the `HH:MM:SS` part of a given `Date` object. - -```js -const getColonTimeFromDate = date => date.toTimeString().slice(0, 8); -``` - -```js -getColonTimeFromDate(new Date()); // "08:38:00" -``` - -
[⬆ back to top](#table-of-contents) - - -### getMeridiemSuffixOfInteger - -Converts an integer to a suffixed string, adding `am` or `pm` based on its value. - -Use the modulo operator (`%`) and conditional checks to transform an integer to a stringified 12-hour format with meridiem suffix. - -```js -const getMeridiemSuffixOfInteger = num => - num === 0 || num === 24 - ? 12 + 'am' - : num === 12 ? 12 + 'pm' : num < 12 ? num % 12 + 'am' : num % 12 + 'pm'; -``` - -```js -getMeridiemSuffixOfInteger(0); // "12am" -getMeridiemSuffixOfInteger(11); // "11am" -getMeridiemSuffixOfInteger(13); // "1pm" -getMeridiemSuffixOfInteger(25); // "1pm" -``` - -
[⬆ back to top](#table-of-contents) - ## Collaborators diff --git a/docs/index.html b/docs/index.html index bfc5d3051..627dacfb2 100644 --- a/docs/index.html +++ b/docs/index.html @@ -50,7 +50,7 @@ scrollToTop(); } }, false); - }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

ary

Creates a function that accepts up to n arguments, ignoring any additional arguments.

Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).

const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
+      }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

ary

Creates a function that accepts up to n arguments, ignoring any additional arguments.

Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).

const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
 
const firstTwoMax = ary(Math.max, 2);
 [[2, 6, 'a'], [8, 4, 6], [10]].map(x => firstTwoMax(...x)); // [6, 8, 10]
 

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
@@ -770,9 +770,19 @@ document.bodyShow examples
formatDuration(1001); // '1 second, 1 millisecond'
 formatDuration(34325055574); // '397 days, 6 hours, 44 minutes, 15 seconds, 574 milliseconds'
+

getColonTimeFromDate

Returns a string of the form HH:MM:SS from a Date object.

Use Date.toString() and String.slice() to get the HH:MM:SS part of a given Date object.

const getColonTimeFromDate = date => date.toTimeString().slice(0, 8);
+
getColonTimeFromDate(new Date()); // "08:38:00"
 

getDaysDiffBetweenDates

Returns the difference (in days) between two dates.

Calculate the difference (in days) between two Date objects.

const getDaysDiffBetweenDates = (dateInitial, dateFinal) =>
   (dateFinal - dateInitial) / (1000 * 3600 * 24);
 
getDaysDiffBetweenDates(new Date('2017-12-13'), new Date('2017-12-22')); // 9
+

getMeridiemSuffixOfInteger

Converts an integer to a suffixed string, adding am or pm based on its value.

Use the modulo operator (%) and conditional checks to transform an integer to a stringified 12-hour format with meridiem suffix.

const getMeridiemSuffixOfInteger = num =>
+  num === 0 || num === 24
+    ? 12 + 'am'
+    : num === 12 ? 12 + 'pm' : num < 12 ? num % 12 + 'am' : num % 12 + 'pm';
+
getMeridiemSuffixOfInteger(0); // "12am"
+getMeridiemSuffixOfInteger(11); // "11am"
+getMeridiemSuffixOfInteger(13); // "1pm"
+getMeridiemSuffixOfInteger(25); // "1pm"
 

tomorrow

Results in a string representation of tomorrow's date. Use new Date() to get today's date, adding one day using Date.getDate() and Date.setDate(), and converting the Date object to a string.

const tomorrow = () => {
   let t = new Date();
   t.setDate(t.getDate() + 1);
@@ -1875,14 +1885,4 @@ Logs: {
 yesNo('yes'); // true
 yesNo('No'); // false
 yesNo('Foo', true); // true
-

Uncategorized

getColonTimeFromDate

Returns a string of the form HH:MM:SS from a Date object.

Use Date.toString() and String.slice() to get the HH:MM:SS part of a given Date object.

const getColonTimeFromDate = date => date.toTimeString().slice(0, 8);
-
getColonTimeFromDate(new Date()); // "08:38:00"
-

getMeridiemSuffixOfInteger

Converts an integer to a suffixed string, adding am or pm based on its value.

Use the modulo operator (%) and conditional checks to transform an integer to a stringified 12-hour format with meridiem suffix.

const getMeridiemSuffixOfInteger = num =>
-  num === 0 || num === 24
-    ? 12 + 'am'
-    : num === 12 ? 12 + 'pm' : num < 12 ? num % 12 + 'am' : num % 12 + 'pm';
-
getMeridiemSuffixOfInteger(0); // "12am"
-getMeridiemSuffixOfInteger(11); // "11am"
-getMeridiemSuffixOfInteger(13); // "1pm"
-getMeridiemSuffixOfInteger(25); // "1pm"
-
\ No newline at end of file +
\ No newline at end of file