Sep 9, 2021 JavaScript
How to calculate the number of months between two dates in JavaScript

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.

Let's define this short function:

const monthDiff = (startDate, endDate) => Math.max(0, (endDate.getFullYear() - startDate.getFullYear()) * 12 - startDate.getMonth() + endDate.getMonth());

Sep 9, 2021 JavaScript
How to compare two dates in JavaScript

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.

Let's define this short function:

const compare = (a, b) => a.getTime() > b.getTime();

Sep 9, 2021 JavaScript
How to convert a date to yyyy-mm-dd format in JavaScript

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.

Let's define this short function:

const formatYmd = date => date.toISOString().slice(0, 10);

Sep 9, 2021 JavaScript
How to convert seconds to hh:mm:ss format in JavaScript

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.

Let's define this short function:

const formatSeconds = s => new Date(s * 1000).toISOString().substr(11, 8);

Sep 9, 2021 JavaScript
How to extract year, month, day, hour, minute, second and millisecond from a date in JavaScript

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.

Let's define this short function:

const extract = date => date.toISOString().split(/[^0-9]/).slice(0, -1);

Sep 9, 2021 JavaScript
How to format a date for the given locale in JavaScript

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.

Let's define this short function:

const format = (date, locale) => new Intl.DateTimeFormat(locale).format(date);