loading
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
var fs = require("fs");

// fs.readFile("events.json", function (err, data) {
//     if (err) throw err;

//     console.log(data.toString());
// });

const readFile = (path, opts = 'utf8') =>
  new Promise((resolve, reject) => {
    fs.readFile(path, opts, (err, data) => {
      if (err) reject(err)
      else resolve(data)
    })
  });


const main = async () => {
  try {

    const fileContent = await readFile("events.json");

    const eventsArray = JSON.parse(fileContent);

    const currentMonth = new Date().getMonth() + 1;

    const currentYear = new Date().getFullYear();

    events = eventsArray.filter(event => {
      var [year, month] = event.date.split('-');

      // Or, var month = e.date.split('-')[1];
      return (currentMonth === +month) && (currentYear == year);
    });

    console.log(events);

  } catch (error) {
    console.log(error);
  }

};

main();
node v10.15.2 linux/amd64