13
snippets/getColonTimeFromDate.md
Normal file
13
snippets/getColonTimeFromDate.md
Normal file
@ -0,0 +1,13 @@
|
||||
### 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"
|
||||
```
|
||||
16
snippets/getMeridiemSuffixOfInteger.md
Normal file
16
snippets/getMeridiemSuffixOfInteger.md
Normal file
@ -0,0 +1,16 @@
|
||||
### 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"
|
||||
```
|
||||
Reference in New Issue
Block a user