diff --git a/README.md b/README.md index 962ea4fdc..2543c7f4a 100644 --- a/README.md +++ b/README.md @@ -430,6 +430,16 @@ average(1, 2, 3); +### _Uncategorized_ + +
+View contents + +* [`getColonTimeFromDate`](#getcolontimefromdate) +* [`getMeridiemSuffixOfInteger`](#getmeridiemsuffixofinteger) + +
+ --- ## 🔌 Adapter @@ -7888,6 +7898,48 @@ 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 9c865ea22..0cb5c6ee1 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);
@@ -1875,4 +1875,14 @@ Logs: {
 yesNo('yes'); // true
 yesNo('No'); // false
 yesNo('Foo', true); // true
-
\ No newline at end of file +

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 diff --git a/snippets/getMeridiemSuffixOfInteger.md b/snippets/getMeridiemSuffixOfInteger.md index 03b529e48..b9d218279 100644 --- a/snippets/getMeridiemSuffixOfInteger.md +++ b/snippets/getMeridiemSuffixOfInteger.md @@ -5,7 +5,10 @@ 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"; +const getMeridiemSuffixOfInteger = num => + num === 0 || num === 24 + ? 12 + 'am' + : num === 12 ? 12 + 'pm' : num < 12 ? num % 12 + 'am' : num % 12 + 'pm'; ``` ```js diff --git a/tag_database b/tag_database index 4a5a0f084..7fbb7da63 100644 --- a/tag_database +++ b/tag_database @@ -76,7 +76,9 @@ functions:object,function gcd:math,recursion geometricProgression:math get:object +getColonTimeFromDate:uncategorized getDaysDiffBetweenDates:date +getMeridiemSuffixOfInteger:uncategorized getScrollPosition:browser getStyle:browser,css getType:type