最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How can I get the weekday from an users input date? - Stack Overflow

matteradmin7PV0评论

function date() {
            var input = document.getElementById("input");

        }
<form>
    <input type="date" placeholder="dd:mm:yy" id="input"/>
    <input type="button" value="weekday" onclick="date()"/>
</form>
    <p id="output">

    </p>

function date() {
            var input = document.getElementById("input");

        }
<form>
    <input type="date" placeholder="dd:mm:yy" id="input"/>
    <input type="button" value="weekday" onclick="date()"/>
</form>
    <p id="output">

    </p>

How can I get the Weekday? I have tried many things and searched on google but haven't found anything. Thanks for help!

Share Improve this question edited Apr 28, 2016 at 18:28 André Kool 4,97812 gold badges35 silver badges44 bronze badges asked Apr 28, 2016 at 17:40 N.EsterlN.Esterl 111 silver badge2 bronze badges 1
  • Read : What should I do when someone answers my question? – Ani Menon Commented May 4, 2016 at 4:25
Add a ment  | 

4 Answers 4

Reset to default 3

Code which takes inputs in the format mm/dd/yyyy :

<script>
  function day_of_week() {
    var weekday = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

    var d = document.getElementById('date_input').valueAsDate;
    var n = d.getUTCDay()
    document.getElementById("output").innerHTML = weekday[n];
  }

</script>

<form>
  <input type="date" placeholder="dd:mm:yy" id="date_input" />
  <input type="button" value="Get Weekday" onclick="day_of_week()" />
</form>
<p id="output">

</p>

You're getting the element, you just need to grab the value.

var weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

function date() {
  var input = document.getElementById("input");
  var date = new Date(input.value);
  var weekday = date.getDay();

  var output = document.getElementById("output");
  output.text = weekdays[weekday];
}

You can do something like this:

function date() {
  var input = document.getElementById("input").value;
  var date = new Date(input).getUTCDay();
  
  var weekday = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
  
  document.getElementById('output').textContent = weekday[date];
}
<form>
  <input type="date" placeholder="dd:mm:yy" id="input" />
  <input type="button" value="weekday" onclick="date()" />
</form>
<p id="output">

</p>

Whenever you use the HTML 5 input type = "date" format for date picker, you can directly get the value in date format,so no need to convert that to date format , also to set the value in your p tag with id="output" you can use the textContent method as shown below:

<form>
  <input type="date" placeholder="dd:mm:yy" id="selectedDate" />
  <input type="button" value="weekday" onclick="date()" />
</form>
<p id="output"></p>

function date() {
  var inputDate = document.getElementById("selectedDate").valueAsDate;
  var day = inputDate.getDay();
  var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  document.getElementById('output').textContent = (days[day]);
}

The getDay() function for date, gives you the number of the weekday of a given date,which is used as index position in weekdays names array.

Post a comment

comment list (0)

  1. No comments so far