0

Please look at the format of these date time values:

var from = "2012-01-13 T11:00:00";
var to = "13 Jan 2012 11:00am";

In the above example, how to write a javascript function which converts a value in "from" format to a value in "to" format, both are datetime values in text obvioulsy.

var to = convertDateTime(from);
function convertDateTime(from)
{
// how to implement this?
}

Many thanks,

The Light
  • 26,341
  • 62
  • 176
  • 258
  • 1
    possible duplicate of [How can I convert string to datetime with format specification in JavaScript?](http://stackoverflow.com/questions/476105/how-can-i-convert-string-to-datetime-with-format-specification-in-javascript) – Mahmoud Gamal Feb 29 '12 at 12:44
  • Hope this post will be helpful for you . http://stackoverflow.com/questions/758999/converting-dates-in-javascript – Govind Feb 29 '12 at 12:47

2 Answers2

2

look at this article : http://blog.stevenlevithan.com/archives/date-time-format

you must convert your string object into date type and then retransform it to string using date format

Jerome Cance
  • 8,103
  • 12
  • 53
  • 106
1

You can use a library like XDate that will handle this for you.

There is no need to reinvent the wheel. When working with dates, you can run into several browser inconsistencies.

kapa
  • 77,694
  • 21
  • 158
  • 175
  • This looks like a great library with a lot of useful functionality, but is probably overkill for most sites. `Date` doesn't suffer from cross-browser differences. AFAIK it's well implemented across all major browsers. – wheresrhys Feb 29 '12 at 13:02
  • @wheresrhys The recognized date formats a Date object can be initialized from differs a lot among browsers normally supported nowadays. – kapa Feb 29 '12 at 13:10
  • I never knew that. Do you know of any sources documenting this? The closest I've found is http://stackoverflow.com/questions/5418631/what-string-date-format-will-javascripts-parse-recognize, but it doesn't come close to saying which formats work where. – wheresrhys Feb 29 '12 at 13:19
  • @wheresrhys Unfortunately not. It would be great to have a table of browsers / supported date formats. – kapa Feb 29 '12 at 13:23