Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getDay() code showing wrong day Javascript

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"

console screenshot

like image 323
Jindi Avatar asked Jun 23 '26 15:06

Jindi


2 Answers

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);
like image 199
Chris Happy Avatar answered Jun 26 '26 06:06

Chris Happy


In Javascript Sunday = 0, Monday = 1....Saturday = 6. Change you weekday array as follows

var weekday = ['Sun', 'Mon', 'Tues', 'Wed', 'Thur', 'Fri', 'Sat'];
like image 30
vabii Avatar answered Jun 26 '26 06:06

vabii



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!