-1

I have a date with the format "Wed Jun 05 2013 00:00:00 GMT+0100 (CET)", and I want to get it in the yyyy-mm-dd format.

I tried this:

var year = mydate.getFullYear();
var month = mydate.getMonth();
var day = mydate.getDay(); 

I got the year and the month, but I can’t get the day.

Ry-
  • 218,210
  • 55
  • 464
  • 476
Mohamed Omezzine
  • 1,074
  • 3
  • 15
  • 28
  • 2
    [`getDay`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDay), [`getDate`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDate) – Ry- Jun 02 '13 at 14:37

1 Answers1

2

You can use mydate.getDate() to get the day-of-month number.

To get the formatting you want, you can do this.

var year = mydate.getFullYear();
var month = ("0" + mydate.getMonth()).slice(-2);
var day = ("0" + mydate.getDate()).slice(-2); 

var formatted = [year, month, day].join("-");
  • 1
    Look here for further explanation https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#Methods – user1983983 Jun 02 '13 at 14:38