1

Part of the form I'm creating has two fields, a day field and a date field. The user will input a date in field 1 (Date) and field 2 (Day) will automatically be populated with the day of the week for that date. I've been trying to piece together the code for the things I need to do (auto-populating a field, returning a day of the week, etc) but it doesn't seem to be working out for me. I'm using Javascript (read: no JQuery).

Here are is the relevant portion of my HTML:

<label>
    <span>input date</span>
    <input type="date" id="date" onChange="getBegDay()">
</label>
<label>
    <span>return day</span>
    <input readonly type="text" id="day">
</label>

Here is the relevant portion of my Javascript:

function getBegDay() {
    var v = document.getElementById("date").value;
    var n = v.split('-');
    var y = n[0];
    var m = n[1];
    var d = n[2];
    var g = new date(y,m,d);
    var weekday = new array(7);
        var weekday[0] = "Sunday";
        var weekday[1] = "Monday";
        var weekday[2] = "Tuesday";
        var weekday[3] = "Wednesday";
        var weekday[4] = "Thursday";
        var weekday[5] = "Friday";
        var weekday[6] = "Saturday";
    var wd = weekday[g.getDay()];
    document.getElementById('day').value = wd;
1
  • What specifically doesn't seem to be working? Commented Sep 10, 2014 at 1:13

3 Answers 3

1

There are three mistakes. First var weekday[0] = "Sunday";...var weekday[6] = "Staturday"; you should remove var, like this:

weekday[0]="Sunday";weekday[1]="Monday";

Second error:

new date(y,m,d) should be new Date(y,m,d).

Third error:

new array(7) should be new Array(7).

Sign up to request clarification or add additional context in comments.

3 Comments

OMG THANK YOU! I knew it had to be something simple like that. It works perfectly now.
Okay, I spoke too soon. one more issue. I thought I had all the days properly marked in the array, but all the days are returning as two days after the selected day. For instance today (Tuesday, 9/9/2014) is returning "Thursday", Wednesday, 9/10/2014 is returning "Friday". I realize I could just change the numbers in the array, but I want to know what's going on for the future.
@StateofDK var m = n[1]; should been var m = n[1]-1; the moth is between 0 and 11 not 1-12
0
var getDate = function() {
    document.getElementById("day").value = [
      "Sunday", 
      "Monday", 
      "Tuesday", 
      "Wednesday", 
      "Thursday",     
      "Friday", 
      "Saturday"][new Date(Date.parse(event.target.value || event.srcElement.value)).getDay()];
}

1 Comment

Readability, ability to follow the logic & debug it beat brevity any day. This answer would be much better if it used temporary variables.. so engineers can follow & debug it.
0

This is the best way, you can use it with all input type date, passing the value by parameters and returning the value to any variable.

var valueDate = document.getElementById("txtDate").value;
var dayOfWeek = getDayOfWeek(valueDate);

function getDayOfWeek(valueDateParam) {
    var v = valueDateParam;
    var n = v.split('-');
    var y = n[0];
    var m = n[1];
    var d = n[2];
    var g = new Date(valueDateParam);
    var weekday = new Array(7);
    weekday[0] = "Monday";
    weekday[1] = "Tuesday";
    weekday[2] = "Wednesday";
    weekday[3] = "Thursday";
    weekday[4] = "Friday";
    weekday[5] = "Saturday";
    weekday[6] = "Sunday";
    var wd = weekday[g.getDay()];

    return wd;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.