-2

I have a website with an automatic updating footer, where you can find the date. But the footer gives the complete wrong date. This is the code behind it:

$(function(){
    var now = new Date();
    var mm = now.getMonth() + 1;
    $("#footer").html(`© NekoLuka ${now.getFullYear()}/${mm}/${now.getDay()}`);
}

Today is 2018/10/24, But the footer gives 2018/10/3.

Does anyone know how to solve this?

  • 5
    Your first stop should be [the documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDay), which tells you that `getDay` returns the **day of the week**. You're looking for [`getDate`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDate), which returns the day of the month. – T.J. Crowder Oct 24 '18 at 12:08
  • now.getDay() // returns day of week;, use now.getDate(); – Saad Mehmood Oct 24 '18 at 12:08
  • 3
    https://www.copyrightlaws.com/copyright-notice-year/ — Your copyright notice should display the date of first publication, not today's date. – Quentin Oct 24 '18 at 12:09
  • Possible duplicate of [How to format a JavaScript date](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date) – Mark Baijens Oct 24 '18 at 13:00

5 Answers5

5

Look at the documentation on MDN.

You are using getDay:

Returns the day of the week (0-6) for the specified date according to local time.

You should be using getDate:

Returns the day of the month (1-31) for the specified date according to local time.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
3

now.getDay() gets the date of the week, you need now.getDate() to get the current day

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
gugateider
  • 1,983
  • 2
  • 13
  • 17
1

The date is coming wrong because you are using getDay to get current date instead of getDate

replace now.getDay() with now.getDate()

Aayush Sharma
  • 779
  • 4
  • 20
1

The problem was that I used the getDay() instead of the getDate(). When I used the getDate(), the script worked.

roschach
  • 8,390
  • 14
  • 74
  • 124
0

Use now.getDate() instead of now.getDay()

Rajkumar A
  • 87
  • 5