admin管理员组文章数量:1022949
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.
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.
本文标签: javascriptHow can I get the weekday from an users input dateStack Overflow
版权声明:本文标题:javascript - How can I get the weekday from an users input date? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745594513a2158084.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论