GetDay () code showing wrong day Javascript

This is my first time (novice yep JS), using the function new Date()

and getDay()

in Javascript. The current date is given correctly, but it gives me the day on Friday, even thought it shows the day on Thursday if I get the current date. I was doing a check of previous questions here, but couldn't see what it really explained. It gives the same result in both Chrome and Firefox. I have also verified that my computer is set to the correct time zone, GMT 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);
      

Run codeHide result


Displaying time and day in the console:

"Thu May 11, 2017 22:25:15 GMT + 0100 (BST) Fri"

console screenshot

+3


source to share


2 answers


Change the array to ['Sun', 'Mon', 'Tues', 'Wed', 'Thur', 'Fri', 'Sat']

.

(Sunday is the first day of the week if Javascript has a word in it;)



var currentDate = new Date();

var weekday = ['Sun', 'Mon', 'Tues', 'Wed', 'Thur', 'Fri', 'Sat'];

var day = weekday[currentDate.getDay()];

console.log(day);
      

Run codeHide result


+4


source


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



var weekday = ['Sun', 'Mon', 'Tues', 'Wed', 'Thur', 'Fri', 'Sat'];

      

+1


source







All Articles