0

I have tickets object and each ticket has its own date:

tickets: [
           {
             id: 0,
             time: "09:45",
             date: new Date("2021-01-01"),
             name: "Swimming in the hillside pond",
           },
           {
             id: 1,
             time: "09:45",
             date: new Date("2021-01-08"),
             name: "Swimming in the hillside pond",
           },
         ],

So what I am trying to do is to find the number of the week of each ticket, but also if the week number is different than the current ones. For example, if the ticket date is 01.01 2021, the number of the week should be 1 and if it is 08.01.2021, it should be 2. So if I have 3 tickets and if the dates for these tickets are [01.01.2021, 08.01.2021, 09.01.2021], I want to have an array like [1,2].

For this, I created function:

currentNumberOfWeek(tickets) {
  return tickets.find((ticket, result) => {
    const oneJan = new Date(ticket.date.getFullYear(), 0, 1);
    const numberOfDays = Math.floor((ticket.date - oneJan) / (24 * 60 * 60 * 1000));
    result = Math.ceil((ticket.date.getDay() + 1 + numberOfDays) / 7);
    console.log(result);
    return result;
  });
},

But first, it returns the ticket not the result and also, in console it doesn't show the right number of the week.

Could you please have a look? Thanks...

Nikola Pavicevic
  • 21,952
  • 9
  • 25
  • 46
magic bean
  • 787
  • 12
  • 39
  • Does this answer your question? [Get week of year in JavaScript like in PHP](https://stackoverflow.com/questions/6117814/get-week-of-year-in-javascript-like-in-php) – derpirscher Aug 05 '21 at 08:22
  • I believe your answer to the question is already available here: https://stackoverflow.com/questions/3280323/get-week-of-the-month – Chhun Socheat Aug 05 '21 at 08:23
  • True, there are some solutions but as I told you in my question, I am also having some problems with finding the function. So I don't know how to return the result. – magic bean Aug 05 '21 at 08:26
  • you're never returning anything in the `.find` callback, so of course nothing is matched - read how `find` is used in this *[documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find)* – Bravo Aug 05 '21 at 08:26
  • What are you trying to achieve with the argument `result` for the callback function of `find()`? The second argument is the index of each iterated element, not a reference to a result variable. See [`Array.prototype.find()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find) – Constantin Groß Aug 05 '21 at 08:29
  • Also, are you trying to calculate the week number for all of the tickets or a single ticket? Because you pass `tickets` (plural argument name) to `currentNumberOfWeek()` (singular method name), then use `filter()` but don't specify what to filter for. It's very confusing, and it would help a lot if you could add a description of what you're actually trying to do. It could be an [XY problem](https://xyproblem.info/) after all. – Constantin Groß Aug 05 '21 at 08:34
  • I updated my question. So what I am trying to do is create an array for the week number of the tickets. So if I have 3 tickets and if the dates for these tickets are [01.01.2021, 08.01.2021, 09.01.2021], I want to have an array like [1,2]. – magic bean Aug 05 '21 at 08:39

1 Answers1

1

If you only want to find week numbers of your ticket

const tickets = [
    {
        id: 0,
        time: "09:45",
        date: new Date("2021-01-01"),
        name: "Swimming in the hillside pond",
    },
    {
        id: 1,
        time: "09:45",
        date: new Date("2021-01-08"),
        name: "Swimming in the hillside pond",
    },
    {
        id: 1,
        time: "09:45",
        date: new Date("2021-01-12"),
        name: "Swimming in the hillside pond",
    },
]


let result = new Set()

function currentNumberOfWeek(arr) {
  arr.forEach(t => {
    let res = getWeekNr(t.date)
    result.add(res)
  })
}

function getWeekNr (date) {
  const oneJan = new Date(date.getFullYear(), 0, 1);
  const numberOfDays = Math.floor((date - oneJan) / (24 * 60 * 60 * 1000));
  let weekNr = Math.ceil((date.getDay() + 1 + numberOfDays) / 7);
  return weekNr;
}
currentNumberOfWeek(tickets)
console.log([...result]);
Nikola Pavicevic
  • 21,952
  • 9
  • 25
  • 46