0

I have:

(new Date()).toISOString().slice(0, 10)

I want it returns without UTC:

2013-08-31

I try:

new Date().getYear() + "-" + new Date().getMonth() + "-" + new Date().getDay()

But it does not work. It returns: 113-7-6.

Thanks a lot.

Francois
  • 275
  • 2
  • 4
  • 11
  • 1
    **Please** take the time to [read some documentation for the three `Date` methods you're using,](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) which should make it quite obvious why you're not getting the result you expect. – Matt Ball Sep 01 '13 at 02:04

1 Answers1

1
  • Date.getYear() returns the year (usually 2-3 digits) in the specified date according to local time, and is deprecated. Use Date.getFullYear() instead.
  • Date.getMonth() returns a zero-indexed value, so you need to add 1 to get 8.
  • Date.getDay() returns zero-indexed day of the week. Use Date.getDate() instead to get 31.

You'll need to zero-pad 8 to get a string with 08 in it, by the way. Or you could simplify everything and use a date formatting library – you'll still need to read documentation, though.

Recommended reading:

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710