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
1 Answer 1
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
Alexander Nenashev
26.2k3 gold badges11 silver badges29 bronze badges
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.Alexander Nenashev
@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 🤷♂️
Alexander Nenashev
@mplungjan it's definitely not a typo, but a bug
mplungjan
We are missing the "this is trivial" and it is likely a dupe too
lang-js
[6, 5].includes("6")is false...===, so the data type of the values (string versus number) matters.@jonrsharpe,console.log (weekends_test.includes(Number(day_of_week_curr)))solves this problemDateobject 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.