1

I am receiving date from back end in 2017-03-02T08:12:22.997000+00:00 this format.

To display this date in specified format I am doing new Date('2017-03-02T08:12:22.997000+00:00').toLocaleString() that gives 3/2/2017, 1:42:22 PM

There's one functionality which is sorting this formatted output. Agenda is to sort the output based on 'date' type. But since I am using toLocaleString() method for formatting, sorting is done based on 'string' type.

Is there any solution where I can achieve 3/2/2017, 1:42:22 PM format and type will be a Date object?

Or a date format where I can see date along with time excluding GMT part? (like toUTCString())

Or any method from moment will work?

VincenzoC
  • 24,850
  • 12
  • 71
  • 90
ManjushaDC
  • 125
  • 2
  • 13
  • have a look over this http://stackoverflow.com/questions/1576753/parse-datetime-string-in-javascript – user7417866 Mar 03 '17 at 17:32
  • 2
    This seems to be a combination of "[*how do I parse an ISO 8601 string to a date*](http://stackoverflow.com/questions/4829569/help-parsing-iso-8601-date-in-javascript)", "[*how do I sort dates*](http://stackoverflow.com/questions/26735854/how-to-return-the-lowest-date-value-and-highest-date-value-from-an-array-in-java/26736729#26736729)" and "[*how do I format a date*](http://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript?s=1|8.9678)". Since the initial dates are strings, you can sort those, then use a Date object for formatting. – RobG Mar 03 '17 at 23:11
  • Note that *toLocaleString* is entirely implementation dependent and produces different results in different hosts. – RobG Mar 03 '17 at 23:14
  • any comment or feedback on my solution @ManjiM – MGA Mar 08 '17 at 05:17

1 Answers1

0

you can use moment.js which will give you moment object.

first convert date to IsoDate as moment.js latest version has deprecated moment constructor with illegal date string ( more info here )

var isoDate = new Date('2017-03-02T08:12:22.997000+00:00').toISOString(),
    formatedDate = moment(isoDate).format('DD/MM/YYYY, HH:MM:SS A');

// formatedDate : "02/03/2017, 13:03:99 PM"

you may use Timezone for better handling of time -

moment(isoDate).tz('timezoneValue').format('DD/MM/YYYY, HH:MM:SS A');
MGA
  • 461
  • 2
  • 11