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
4 Answers
Reset to default 3Code 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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745594516a4635029.html
评论列表(0条)