Reputation: 49
This is my first time (yep JS newbie) using new Date()
and getDay()
functions in Javascript. The current date given is correct, yet it gives me the day as Friday even thought it shows the day as Thursday if I get current date. I did do a check of previous questions on here but couldn't see any that really explained it. It gives the same result in both Chrome and Firefox. I also checked that my computer is set to the correct time zone, UK GMT.
Code:
var currentDate = new Date();
console.log(currentDate);
var weekday = ['Mon', 'Tues', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun'];
var day = weekday[currentDate.getDay()];
console.log(day);
Time and day show in Console:
"Thu May 11 2017 22:25:15 GMT+0100 (BST) Fri"
Upvotes: 3
Views: 1863
Reputation: 7295
Change your array to ['Sun', 'Mon', 'Tues', 'Wed', 'Thur', 'Fri', 'Sat']
.
(Sunday is the first day in the week if Javascript has a say in it ;)
var currentDate = new Date();
var weekday = ['Sun', 'Mon', 'Tues', 'Wed', 'Thur', 'Fri', 'Sat'];
var day = weekday[currentDate.getDay()];
console.log(day);
Upvotes: 4
Reputation: 521
In Javascript Sunday = 0, Monday = 1....Saturday = 6. Change you weekday array as follows
var weekday = ['Sun', 'Mon', 'Tues', 'Wed', 'Thur', 'Fri', 'Sat'];
Upvotes: 1