Coding newbie Coding newbie - 4 years ago 119
Javascript Question

javascript parse weekday value from input type="date"

I'm trying to get values from html input type date and time using javascript and I was wondering if someone can help me to figure out how to parse weekday as well.

Below is what I have so far,

function showDate_(getdate) {
var dateformat = getdate.split('-')
var displaydate = document.getElementById('time')

if (displaydate) {
displaydate.textContent = dateformat[1] + '-' + dateformat[2];


<input onchange="showDate(this.value)" type="date" class="date" />
<div id="time"></div>

When selecting the time from calendar, the output will be something like 02-06. How can I include weekday, for example Monday, 02-06?

Any help would be greatly appreciated.

Answer Source

You can try like this,

function getDay() {
  const week_of_day_arr = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
  var dateString = document.getElementById('date').value
  var day = week_of_day_arr[new Date(dateString).getDay()];

document.getElementById('btn').addEventListener("click", getDay)
<input type="text" id="date" placeholder="Enter Date (YYYY-MM-DD)" />
<button id="btn">Get Day</button>

to fetch day of week from current date which you will pass.

your_date_string is your date in the form of yyyy-mm-dd.

Give it a try. It will work.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download