-2

I call php from javascript. Url address is http://xxx.xx.x.xx/wdcalendar/novaoperacia.php?start=Tue Jun 18 18:00:00 UTC+0200 2013. It is not suitable for me. I want to convert do format YYYY-MM-DD. But in js. How can I make it ? Or how can I make it in php ?

luma64
  • 21
  • 3
  • in php it would be as simple as saying date("Y-m-d", strtotime($date)); – Orangepill Jun 17 '13 at 17:53
  • There are a whole heap of answers here on SO if you care to search, here is just one of them: http://stackoverflow.com/questions/17093796/date-formatting-with-without-moment-js/17094020#17094020 – Xotic750 Jun 17 '13 at 17:53

1 Answers1

0

In javascript:

Your input:

var s = "Tue Jun 18 18:00:00 UTC+0200 2013";

Instantiate the Date object:

var d = new Date(s);

Use standard Date methods: (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) using the slice on the last two methods to get 2 digits for each

var yyyy = d.getFullYear();
var mm = ("0" + (d.getMonth() + 1)).slice(-2);
var dd = ("0" + d.getDate()).slice(-2);

The final output declaration:

var out = yyyy + '-' + mm + '-' + dd;
console.log(out);

Outputs "2013-06-18"

intersauce
  • 54
  • 2
  • -1, Relying on the string parsing capabilities of the `Date` object with the OPs date string will fail on a number of browsers. http://dygraphs.com/date-formats.html – Xotic750 Jun 17 '13 at 19:27