Update and rename getQuarterOfYear.md to quarterOfYear.md

This commit is contained in:
Isabelle Viktoria Maciohsek
2020-10-09 10:23:55 +03:00
committed by GitHub
parent 032dc94b49
commit 7d34ba7d64
2 changed files with 23 additions and 23 deletions

View File

@ -1,23 +0,0 @@
---
title: getQuarterOfYear.md
tags: array,intermediate
---
It returns the quarter and year to which the supplied date belongs to.
- getMonth() function return 0 to 11 based on the month and hence I am adding 1 to it.
- getFullYear() function returns the full year of the passed date.
- in the return statement we have used string literals to combine stirng with our variables. we are dividing the month by 3 as we have 4 quarters in a year.
```js
const getQuarterOfYear = (date) => {
const month = new Date(date).getMonth() + 1;
const year = new Date(date).getFullYear();
return `Q${Math.ceil(month / 3)}-${year}`;
};
```
```js
getQuarterOfYear("07/10/2018"); // Q3-2018 (dateformat: mm/dd/yyyy)
getQuarterOfYear("12/12/2020"); // Q4-2020 (dateformat: mm/dd/yyyy)
```

23
snippets/quarterOfYear.md Normal file
View File

@ -0,0 +1,23 @@
---
title: quarterOfYear
tags: date,beginner
---
Returns the quarter and year to which the supplied date belongs to.
- Use `Date.prototype.getMonth()` to get the current month in the range (0, 11), add `1` to map it to the range (1, 12).
- Use `Math.ceil()` and divide the month by `3` to get the current quarter.
- Use `Date.prototype.getFullYear()` to get the year from the given date.
- Omit the argument, `date`, to use the current date by default.
```js
const quarterOfYear = (date = new Date()) => [
Math.ceil((date.getMonth() + 1) / 3),
date.getFullYear(),
];
```
```js
quarterOfYear(new Date('07/10/2018')); // [ 3, 2018 ]
quarterOfYear(); // [ 4, 2020 ]
```