-1

How to split the below date format into day date and time.

Date Format is like "2013-05-07T11:04:00+05:30" I want to display above date like "Tue,7 May 2013, 11.04AM". Please suggest how to do this in java script or jquery

Thanks in Advance.

user2333363
  • 217
  • 2
  • 5
  • 15
  • go through date.js .. https://code.google.com/p/datejs/wiki/APIDocumentation ...come in handy related to javascript Date stuff – bipen May 07 '13 at 09:16
  • 2
    It's already asked on [Format date time JavaScript][1]. Check it out there are some useful answers. [1]: http://stackoverflow.com/questions/9709989/format-date-time-javascript – Ali Bahraminezhad May 07 '13 at 09:17

4 Answers4

2

You can use this very useful little library:

http://momentjs.com/

Example:

moment("2013-05-07T11:04:00+05:30", "MMMM Do YYYY, h:mm:ss a");
Getz
  • 3,983
  • 6
  • 35
  • 52
  • I have used moment as shown in above. i used alert to print.i got "61535982570000". My code is like this " var date=moment("2013-05-07T11:04:00+05:30", "MMMM Do YYYY, h:mm:ss a"); alert(date); The date i am getting dynamically. – user2333363 May 07 '13 at 10:09
  • Try with the format function: moment("2013-05-07T11:04:00+05:30").format("dddd, MMMM Do YYYY, h:mm:ss a"); – Getz May 07 '13 at 11:52
1

You should work with the native Date Object - this is the easiest way to handle the string.

You can do the following:

var date = new Date("2013-05-07T11:04:00+05:30");

now you have several Date methods you can use to format your string and get the information you need, e.g. what you want is:

date.toUTCString()
// output:
"Tue, 07 May 2013 05:34:00 GMT"

You also could use a regex or an external library, but probably the best way (imo!) is to simply work with the Date Object.

Christoph
  • 50,121
  • 21
  • 99
  • 128
1

You might be able to use Date.parse(string), but there is no way to determine what date/time formats a particular JavaScript implementation supports.

Otherwise:

  • Use a regex to break up (and validate) the string, convert each string component into a Number and then pass to the Date constructor taking separate components.
  • Use a library that implements the previous option (eg. see other answer).
Community
  • 1
  • 1
Richard
  • 106,783
  • 21
  • 203
  • 265
  • Any JavaScript implementation that supports ECMAScript 5 will support ISO8601 formatting passed to `Date.parse()`, however others may or may not support this feature (such as IE<=8). For actually formatting the string, using a custom function is recommended, as the native `Date` methods are implementation dependent in their formatting of dates. – Qantas 94 Heavy May 07 '13 at 09:23
0

moment.js could be a good choice for you, for example:

Actual moment:

moment().format('MMMM Do YYYY, h:mm:ss a');

Format:

moment("2013-05-07T11:04:00+05:30", "MMMM Do YYYY, h:mm:ss a");
lfergon
  • 963
  • 15
  • 27