Reputation: 79
My school command is this:
The function-including getLocalDay Advocate Written below is returned the day of the week as integer.
It was 0 Sunday, Monday 1, Tuesday 2 etc ..
In some countries (including The Netherlands) is the first day of the week Monday. Make sure this function THAT THAT's how it works Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday as integers respectively 0 to 6 is returned. So 0 = Monday / Tuesday = 1/2 = Tuesday / Thursday = 3/4 = Friday / 5 = Saturday / Sunday 6 =
I got this
function getLocalDay(date) {
var day = date.getDay();
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";
console.log("Today is " + weekday[3]);
var d=new Date();
console.log(d.getDay());
}
but i want to return numbers from that day.....
Upvotes: 0
Views: 134
Reputation: 65806
You need to incorporated our solutions into the code on your own page. Here, I am showing a sample heading that contains a span
that will be updated with the day number.
window.addEventListener("DOMContentLoaded", function(){
// Get a reference to the <span> element on the page
var label = document.getElementById("day");
// Inject the answer from the function into it:
label.textContent = getLocalDay(new Date());
});
function getLocalDay(date) {
var day = date.getDay();
var weekdays = ["Monday","Tuesday" ,"Wednesday" ,
"Thursday" ,"Friday" ,"Saturday" ,"Sunday" ];
let dayNum = null;
weekdays.forEach(function(d, index, arry){
if(d === weekdays[day - 1]){
dayNum = index;
}
});
return dayNum;
}
<h1>The day number for today is: <span id="day"></span></h1>
Upvotes: 0
Reputation: 1148
It should be
function getLocalDay(date) {
var dayIndex = date.getDay();
var daysInWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
return daysInWeek[dayIndex];
}
ES6 syntax
const getLocalDay = (date) => {
const dayIndex = date.getDay();
const daysInWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
return daysInWeek[dayIndex];
}
Upvotes: 0