0

Today is 07 May 2016, Saturday But when I implement a js practice, the getDay() always return the correct number plus one. So I did this test.

enter image description here

Today is Saturday, so I expect to return 5 rather than 6.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Mangosteen
  • 181
  • 3
  • 10
  • 1
    Read the documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDay – Felix Kling May 06 '16 at 16:20
  • 2
    Welcome to SO. Please visit the [help] and take the [tour] to see what and how to ask. HINT: Search documentation first and then post actual code instead of pictures of code – mplungjan May 06 '16 at 16:22
  • 1
    PS: date.getMonth() will return zero-based months - Jan is 0 – mplungjan May 06 '16 at 16:23
  • I think adding to the confusion is the fact the _date_ is the 7th. The 6 is unrelated to that. You'll get 6 next weekend on Saturday, when the date is the 14th too. – James Thorpe May 06 '16 at 16:23
  • @mplungjan Thank you for your hint and guide. I will learn that now. – Mangosteen May 06 '16 at 16:40

3 Answers3

2

The getDay() method returns the day of the week for the specified date according to local time, where 0 represents Sunday.

Source

So if Sunday is 0 then you get these values for the other days:

  1. Monday
  2. Tuesday
  3. Wednesday
  4. Thursday
  5. Friday
  6. Saturday
Mike Cluck
  • 31,869
  • 13
  • 80
  • 91
  • 1
    Thank you so much. I was in china, Monday is the begin of a week but now I am in Australia, Sunday should be the begin. I have understood it now. – Mangosteen May 06 '16 at 16:23
  • I was recently stuck in the rabbit hole while working with translators on a project I wrote https://blogs.msdn.microsoft.com/global_developer/2011/06/03/what-is-the-first-day-of-the-week-really/ – Jason Sperske May 06 '16 at 16:24
  • 1
    @user3210050: `getDay` will always return `0` for Sunday, no matter where you are. – Felix Kling May 06 '16 at 16:26
  • 1
    @Felix Kling Yes, I did not realise it before.Monday is the first day in my mind before, so I should read the documentation clearly at first. Sorry for my weakness. – Mangosteen May 06 '16 at 16:35
  • Solving this bug now would make a whole lot of code non-backward-compatible. – gurghet Jan 02 '23 at 10:41
0

The day of the week:

var d = new Date(2016, 5, 20); // June 20, 2016
d.getDay();

Starting the count from 0 (Sunday), 1 means Monday and etc...

@Mike C:

  • 0 Sunday

  • 1 Monday

  • 2 Tuesday

  • 3 Wednesday

  • 4 Thursday

  • 5 Friday

  • 6 Saturday

You will have the same situation with month too. The value returned by getMonth() is an integer between 0 and 11. 0 corresponds to January, 1 to February, and so on.

  • 0 January

  • 1 February

  • 2 March

  • 3 April

  • 4 May

  • 5 June

  • 6 July

  • 7 August

  • 8 September

  • 9 October

  • 10 November

  • 11 December

var d = new Date(2016, 5, 20); // June 20, 2016

d.getMonth(); // 5

Why?

As assumed @ChristopherW this can help to reference in an array of names.

var 
  months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
  days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];

var d = new Date(2016, 5, 20); // June 20, 2016

months[d.getMonth()]; // "June"
days[d.getDay()]; // "Monday"

It is very convenient for developers. But there is another reason for zero-indexing. The syntax of JavaScript is actually derived from C. Lets analize C's localtime function.

The C library function struct tm *localtime(const time_t *timer) uses the time pointed by timer to fill a tm structure with the values that represent the corresponding local time. The value of timer is broken up into the structure tm and expressed in the local time zone.

struct tm *localtime(const time_t *timer)

This function returns a pointer to a tm structure with the time information filled in. Following is the tm structure information:

struct tm {
   int tm_sec;         /* seconds,  range 0 to 59          */
   int tm_min;         /* minutes, range 0 to 59           */
   int tm_hour;        /* hours, range 0 to 23             */
   int tm_mday;        /* day of the month, range 1 to 31  */
   int tm_mon;         /* month, range 0 to 11             */
   int tm_year;        /* The number of years since 1900   */
   int tm_wday;        /* day of the week, range 0 to 6    */
   int tm_yday;        /* day in the year, range 0 to 365  */
   int tm_isdst;       /* daylight saving time             */ 
};

As you see month starts from 0 to 11, day of the week from 0 to 6.

Community
  • 1
  • 1
Ali Mamedov
  • 5,116
  • 3
  • 33
  • 47
  • 1
    Javascript is more based on Java than C (there are many languages with C–like syntax), its Date object is almost a copy from Java. – RobG May 07 '16 at 08:03
0

you can simply add 6 and use modulus operator.

var d = new Date(2016, 5, 20);

var weekday = (d.getDay() + 6) % 7;