0

Let's say I have a Date object. Is there a simpler way to get the YYYY-MM-DD format than doing the following?

let val = new Date(2014,1,1);
console.log(val.toJSON().split('T')[0]);
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
David542
  • 104,438
  • 178
  • 489
  • 842
  • 1
    `val.toJSON().slice(0,10)` Be aware that this will give UTC date, not local date. That can be different at some points in time. – trincot May 16 '22 at 19:12
  • 2
    Please do basic research before asking, e.g. there are 52 (!) answers to this at https://stackoverflow.com/q/23593052/3001761. – jonrsharpe May 16 '22 at 19:13
  • @trincot are there any differences between `.toISOString()` and `toJSON()` in the above ? – David542 May 16 '22 at 19:15
  • 1
    No, there aren't. `toJSON` uses `toISOString`. – trincot May 16 '22 at 19:17
  • 1
    [_"Calling `toJSON()` returns a string (using `toISOString()`)..."_](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toJSON#description) – jonrsharpe May 16 '22 at 19:17

0 Answers0