Release the latest generator

This commit is contained in:
fechin committed 2022-11-16 14:10:09 +08:00
1 parent b520119d38
commit 15c93e54ee
484 files changed
+4969 -856

No files matched your search

@@ -0,0 +1,36 @@
---
title: How to add am pm suffix to an hour in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Date Time
layout: note
---
In this Article we will go through how to add am pm suffix to an hour only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const suffixAmPm = h => `${h % 12 === 0 ? 12 : h % 12}${h < 12 ? 'am' : 'pm'}`;
```
Parameters `h` is an hour number between 0 and 23.
### Example
```js {.wrap}
suffixAmPm(0); // '12am'
suffixAmPm(5); // '5am'
suffixAmPm(12); // '12pm'
suffixAmPm(15); // '3pm'
suffixAmPm(23); // '11pm'
```
@@ -0,0 +1,28 @@
---
title: How to calculate the number of difference days between two dates in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Date Time
layout: note
---
In this Article we will go through how to calculate the number of difference days between two dates only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const diffDays = (date, otherDate) => Math.ceil(Math.abs(date - otherDate) / (1000 * 60 * 60 * 24));
```
### Example
```js {.wrap}
diffDays(new Date('2014-12-19'), new Date('2020-01-01')); // 1839
```
@@ -0,0 +1,28 @@
---
title: How to calculate the number of months between two dates in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Date Time
layout: note
---
In this Article we will go through how to calculate the number of months between two dates only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const monthDiff = (startDate, endDate) => Math.max(0, (endDate.getFullYear() - startDate.getFullYear()) * 12 - startDate.getMonth() + endDate.getMonth());
```
### Example
```js {.wrap}
monthDiff(new Date('2020-01-01'), new Date('2021-01-01')); // 12
```
@@ -0,0 +1,31 @@
---
title: How to compare two dates in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Date Time
layout: note
---
In this Article we will go through how to compare two dates only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const compare = (a, b) => a.getTime() > b.getTime();
```
Parameters `a` and `b` are `Date` instances.
### Example
```js {.wrap}
compare(new Date('2020-03-30'), new Date('2020-01-01')); // true
```
@@ -0,0 +1,32 @@
---
title: How to convert a date to yyyy-mm-dd format in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Date Time
layout: note
---
In this Article we will go through how to convert a date to yyyy-mm-dd format only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const formatYmd = date => date.toISOString().slice(0, 10);
```
Parameters `date` is a `Date` object.
### Example
```js {.wrap}
formatYmd(new Date()); // 2020-05-06
```
@@ -0,0 +1,45 @@
---
title: How to convert seconds to hh:mm:ss format in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Date Time
layout: note
---
In this Article we will go through how to convert seconds to hh:mm:ss format only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const formatSeconds = s => new Date(s * 1000).toISOString().substr(11, 8);
```
Parameters `s` is number of seconds.
### Or
```js {.wrap}
const formatSeconds = s => (new Date(s * 1000)).toUTCString().match(/(\d\d:\d\d:\d\d)/)[0];
```
### Or
```js {.wrap}
const formatSeconds = s => [parseInt(s / 60 / 60), parseInt(s / 60 % 60), parseInt(s % 60)].join(':').replace(/\b(\d)\b/g, '0$1');
```
### Example
```js {.wrap}
formatSeconds(200); // 00:03:20
formatSeconds(500); // 00:08:20
```
@@ -0,0 +1,28 @@
---
title: How to extract year, month, day, hour, minute, second and millisecond from a date in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Date Time
layout: note
---
In this Article we will go through how to extract year, month, day, hour, minute, second and millisecond from a date only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const extract = date => date.toISOString().split(/[^0-9]/).slice(0, -1);
```
Parameters `date` is a `Date` object.
Parameters `extract` is an array of [year, month, day, hour, minute, second, millisecond].
@@ -0,0 +1,35 @@
---
title: How to format a date for the given locale in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Date Time
layout: note
---
In this Article we will go through how to format a date for the given locale only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const format = (date, locale) => new Intl.DateTimeFormat(locale).format(date);
```
Parameters `locale` is a locale (en-US, pt-BR, for example).
Parameters `date` is a `Date` object.
### Example
```js {.wrap}
format(new Date(), 'pt-BR'); // 06/05/2020
```
@@ -0,0 +1,22 @@
---
title: How to get the current quarter of a date in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Date Time
layout: note
---
In this Article we will go through how to get the current quarter of a date only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const getQuarter = (d = new Date()) => Math.ceil((d.getMonth() + 1) / 3);
```
@@ -0,0 +1,22 @@
---
title: How to get the current timestamp in seconds in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Date Time
layout: note
---
In this Article we will go through how to get the current timestamp in seconds only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const ts = () => Math.floor(new Date().getTime() / 1000);
```
@@ -0,0 +1,32 @@
---
title: How to get the day of the year from a date in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Date Time
layout: note
---
In this Article we will go through how to get the day of the year from a date only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const dayOfYear = date => Math.floor((date - new Date(date.getFullYear(), 0, 0)) / (1000 * 60 * 60 * 24));
```
Parameters `date` is a Date object.
### Example
```js {.wrap}
dayOfYear(new Date(2020, 04, 16)); // 137
```
@@ -0,0 +1,22 @@
---
title: How to get the first date in the month of a date in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Date Time
layout: note
---
In this Article we will go through how to get the first date in the month of a date only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const getFirstDate = (d = new Date()) => new Date(d.getFullYear(), d.getMonth(), 1);
```
@@ -0,0 +1,22 @@
---
title: How to get the last date in the month of a date in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Date Time
layout: note
---
In this Article we will go through how to get the last date in the month of a date only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const getLastDate = (d = new Date()) => new Date(d.getFullYear(), d.getMonth() + 1, 0);
```
@@ -0,0 +1,25 @@
---
title: How to get the month name of a date in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Date Time
layout: note
---
In this Article we will go through how to get the month name of a date only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const getMonthName = date => ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October',' November', 'December'][date.getMonth()];
```
Parameters `date` is a Date object.
@@ -0,0 +1,25 @@
---
title: How to get the number of days in given month in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Date Time
layout: note
---
In this Article we will go through how to get the number of days in given month only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const daysInMonth = (month, year) => new Date(year, month, 0).getDate();
```
Parameters `month` is zero-based index.
@@ -0,0 +1,28 @@
---
title: How to get the timezone string in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Date Time
layout: note
---
In this Article we will go through how to get the timezone string only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const getTimezone = () => Intl.DateTimeFormat().resolvedOptions().timeZone;
```
### Example
```js {.wrap}
getTimezone(); // 'Asia/Saigon'
```
@@ -0,0 +1,28 @@
---
title: How to get the tomorrow date in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Date Time
layout: note
---
In this Article we will go through how to get the tomorrow date only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const tomorrow = (d => new Date(d.setDate(d.getDate() + 1)))(new Date);
```
### Or
```js {.wrap}
const tomorrow = new Date((new Date()).valueOf() + 1000 * 60 * 60 * 24);
```
@@ -0,0 +1,28 @@
---
title: How to get the total number of days in a year in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Date Time
layout: note
---
In this Article we will go through how to get the total number of days in a year only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const numberOfDays = year => (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0)) ? 366 : 365;
```
### Or
```js {.wrap}
const numberOfDays = year => new Date(year, 1, 29).getDate() === 29 ? 366 : 365;
```
@@ -0,0 +1,25 @@
---
title: How to get the weekday of a date in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Date Time
layout: note
---
In this Article we will go through how to get the weekday of a date only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const getWeekday = date => ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][date.getDay()];
```
Parameters `date` is a Date object.
@@ -0,0 +1,28 @@
---
title: How to get the yesterday date in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Date Time
layout: note
---
In this Article we will go through how to get the yesterday date only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const yesterday = (d => new Date(d.setDate(d.getDate() - 1)))(new Date);
```
### Or
```js {.wrap}
const yesterday = new Date((new Date()).valueOf() - 1000 * 60 * 60 * 24);
```
@@ -0,0 +1,22 @@
---
title: How to initialize the current date but set time to midnight in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Date Time
layout: note
---
In this Article we will go through how to initialize the current date but set time to midnight only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const midnightOfToday = () => new Date(new Date().setHours(0, 0, 0, 0));
```
@@ -0,0 +1,26 @@
---
title: How to sort an array of dates in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Date Time
layout: note
---
In this Article we will go through how to sort an array of dates only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const sortDescending = arr => arr.sort((a, b) => a.getTime() > b.getTime());
const sortAscending = arr => arr.sort((a, b) => a.getTime() < b.getTime());
```
Parameters `arr` is an array of `Date` items.