0

What am I doing wrong?

Here is my code:

let weekends_test=[6,5];
let day_of_week_curr=moment.unix(curr_time).format('e'); //returns 6 (I'm using moment.js library)
console.log (weekends_test.includes(day_of_week_curr)) //returns false, but true expected

indexOf instead includes returns "-1"

Maybe other way to find a value exists? Except "for" cycle?

fdomn-m
28.8k8 gold badges39 silver badges70 bronze badges
5
  • 5
    [6, 5].includes("6") is false... Commented Mar 6, 2025 at 12:03
  • 1
    When you format a date, you get a string. The array "search" functions perform comparisons as if they were using ===, so the data type of the values (string versus number) matters. Commented Mar 6, 2025 at 12:06
  • 1
    Thank you so much, @jonrsharpe, console.log (weekends_test.includes(Number(day_of_week_curr))) solves this problem Commented Mar 6, 2025 at 12:07
  • Moment is EOL (end of life), also you can do this fairly easily with a raw Date object i.e. console.log("Is Weekend:", [0, 6].includes(new Date().getDay())) Using a date framework in 2025 might be overkill. The I18n (Intl) packages offer a lot of features now. Commented Mar 6, 2025 at 14:33
  • @MrPolywhirl, I haven't figured out how to use native functions to get a timestamp in a strictly defined timezone. How to get a timestamp when changing between winter and summer time. How to quickly get the start of day timestamp from the current timestamp. Moment.js is still useful. Commented Mar 7, 2025 at 16:54

1 Answer 1

0

moment.format returns a string, so you should convert it to a number:

let day_of_week_curr=parseInt(moment.unix(curr_time).format('e'));
answered Mar 6, 2025 at 12:08
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, Alexander, the code weekends_test.includes(Number(day_of_week_curr)) also solved an issue, My reputation is low, so I cannot mark your answer as useful, but anyway, thank you.
@mplungjan i didnt read the comment, was answering and there's no rule that prohibits posting an answer that already covered in the comments, actually i encounter that's even encouraged 🤷‍♂️
@mplungjan it's definitely not a typo, but a bug
We are missing the "this is trivial" and it is likely a dupe too

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.