2

im stuck at a problem here im getting the date in the following format

Wed May 16 2012 10:22:39 GMT+0500 

i have tried

.toString('dd-MM-yyy');

but to no avail how can i parse the given format into the required one.

John x
  • 4,031
  • 8
  • 42
  • 67
  • how do you get the first format in the first place? – Andreas Wong May 17 '12 at 08:05
  • @NiftyDude i'm using the telerik grid it provides the function to format the date but at the backend the `json` it maintains in that it keeps the date in the above mentioned format and i have to get the date from its hidden store – John x May 17 '12 at 08:07
  • If you already have GMT difference defined you dont need to see anything in brackets – jerrymouse May 17 '12 at 08:07
  • 2
    good read here: http://blog.stevenlevithan.com/archives/date-time-format or seems like this question is asked earlier just a note: http://stackoverflow.com/questions/3965365/convert-date-into-dd-mm-yyyy-format-in-jquery – Tats_innit May 17 '12 at 08:08

4 Answers4

5

You can use jQuery Date Format, it's a separate plugin that allows you to format dates very easily.

<script>
$.format.date("Wed May 16 2012 10:22:39 GMT+0500", "dd-MM-yyyy");
</script>
Sandeep Bansal
  • 6,280
  • 17
  • 84
  • 126
4
function zeroPad(d) {
  return d < 10 ? '0' + d : d;
}

var date = new Date('Wed May 16 2012 10:22:39 GMT+0500'),
    d = zeroPad(date.getDate()),
    m = zeroPad(date.getMonth()+1);

var output = d + '-' + m + '-' + date.getFullYear();
thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
2

There's no built-in way in JS to do date formatting like that. You need read each item manually from the Date object.

var today = new Date();
var day = today.getDate();
...

If have a lot of work requiring date manipulation I suggest you take a look at Sugar JS library.

Makram Saleh
  • 8,613
  • 4
  • 27
  • 44
2

If you happen to already be using jQuery UI's "datepicker", there's a utility function included - $.datepicker.formatdate() - http://docs.jquery.com/UI/Datepicker/formatDate

Alnitak
  • 334,560
  • 70
  • 407
  • 495