Format Javascript date, find day of the week?

Asked

Viewed 1,418 times

2

I have the following problem.

I have a date Picker that gives me the value of the date so : dd-mm-yya (13-05-2016)

But I need to verify which day of the week is ex: Today is Friday. The problem I can’t verify with this format.

I would have to take the value of the field and change it to use New Date() and getDay() to check. Now how do I do it ?

Or do I have a way of checking in that shape ?

Or how to use the method toLocaleDateString

Note need to be in javascript not Jquery, I’m learning JS.

Hugs

2 answers

2

  • I adjusted the answer

  • Thanks, it works well. Too bad it’s with Jquery. I’ve already done it with pure js. Thank you so much for the help.

  • 1

    Oh yes, jQuery is just to select the element, but the focus is to split your date by the '-' and use the parameters in the correct order of the date.

1


My answer to the problem.

var str = campo.value,
    parts = str.split('-'),
    year = parseInt(parts[2], 10),
    month = parseInt(parts[1], 10) - 1,
    day = parseInt(parts[0], 10),
    date = new Date(year, month, day),
    dia = date.getDay();

if (dia == "6") {
    alert("Sábado ");
}
if (dia == "0") {
    alert(" Domingo");
};

Browser other questions tagged

You are not signed in. Login or sign up in order to post.