0

My problem is that I want to get all months that have been in 2020 i.e. January, February, Marts, April, May, June, July and August, and add them to an array.

When I am using code below, I get the number 7 in variable month, which means it's August. Now I want to add 7, 6, 5, 4, 3, 2, 1, 0 to an array. How do I do that?

const d = new Date(); const month = d.getMonth();

I am new to typescript, I hope someone is able to help me. Thanks in advance.

lo7
  • 425
  • 2
  • 8
  • 21

2 Answers2

1

This is not Typescript specific and can be done with plain javascript. If getMonth is 7 and you want [0, 1, 2, 3, 4, 5, 6, 7] then you can use this:

const d = new Date();
const month = d.getMonth();
const monthArray = Array.from(Array(month + 1).keys());

I've used following reference: https://stackoverflow.com/a/33352604/9124424

Stutje
  • 745
  • 6
  • 21
1

Code for the problem using simple for loop and while loop to enter elements in an array](https://i.stack.imgur.com/aJRkX.jpg)

Taha Habib
  • 32
  • 4