0

I'm getting the date format as Tue May 31 2016 18:15:25 GMT-0300 (Hora oficial do Brasil) but I need it in 2016-05-31.

After several tries, on the last hour, I still can't get it right...

I need it to be as a filter for MySQL database.

How may I achieve this? Thanks!

William
  • 1,010
  • 3
  • 21
  • 39

2 Answers2

0

Try this using JavaScript:

var year = date.getFullYear();
  var month = (1 + date.getMonth()).toString();
  month = month.length > 1 ? month : '0' + month;
  var day = date.getDate().toString();
  day = day.length > 1 ? day : '0' + day;
  var formattedDate = year + '-' + month + '-' + day;
  alert(formattedDate);
  return formattedDate;
}

var date1 = new Date();
getFormattedDate(date1);

You can read more about JS date formats here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date. You can also experiment on this plunk here : http://plnkr.co/edit/E7Nm5HlQu3pkYzIPSfVN

Mona
  • 298
  • 3
  • 14
-1

Use the internationalization formatter

new Intl.DateTimeFormat('br').format(new Date(2016,4,31)) // => 2016-05-31

br indicates the Brazilian locale's formatting preferences, you can also use en-us, en-gb, etc. to both format and extract dates in a variety of different locales.

zetavolt
  • 2,989
  • 1
  • 23
  • 33