0

My Web-service returns me date which is like this/Date(1342622718553-0700)/. How can i convert it to NSDate.

I have seen this type of parsing Here and Here but its not for iOS.

Community
  • 1
  • 1
Dilip Manek
  • 9,095
  • 5
  • 44
  • 56
  • 2
    This was what i've used : http://stackoverflow.com/a/6065278/1039901 – Samet DEDE Mar 03 '14 at 13:03
  • possible duplicate of [Parsing JSON dates on IPhone](http://stackoverflow.com/questions/1757303/parsing-json-dates-on-iphone) – Martin R Mar 03 '14 at 13:05
  • Thanx @EPyLEpSY the link you have suggested have perfect answer. – Dilip Manek Mar 03 '14 at 13:14
  • Basically, one needs to separate out the two numbers, divide the first by 1000 (milliseconds to seconds) and feed it to dateWithTimeIntervalSince1960. Then convert the second into a timezone, possibly by multiplying the first 2 digits by 60, adding in the second two, multiplying by 60, incorporating the sign, and using timeZoneForSecondsFromGMT. – Hot Licks Mar 03 '14 at 13:21

1 Answers1

0

The date formate you are parsing is known as ISO8601 DateFormatter, its older date formate and its recommonded not to use in new development. for Your solution you can parse this date by using library class ISO8601DateFormatter Classes

first import ISO8601DateFormatter.h and .m files in ur project and parse your date as follows:

   NSString *myDate = @"/Date(1342622718553-0700)/";
    ISO8601DateFormatter *dateFormatter = [[ISO8601DateFormatter alloc] init];
    [dateFormatter setFormat:ISO8601DateFormatOrdinal];
    NSDate *dateObject = [dateFormatter dateFromString:myDate];

This works for me. You can edit format style according to your requirement. This link gives you detail Check Here for ISO 8601 date formatter

Satish Azad
  • 2,302
  • 1
  • 16
  • 35
  • Thanx for reply. You said `recommonded not to use in new development` can you tell me why? Just for if its harmful than i will tell my web-developer to change it. – Dilip Manek Mar 04 '14 at 04:18
  • Its not a big problem. Actually, ISO 8601 provides a standard unambiguous format (several, actually) for dates and times. Unfortunately, NSDateFormatter in Objective-C alone does not completely support ISO 8601; in particular, it does not support week dates or ordinal dates. So May be it creates some problem for u, (depends upon your requirement). Please check link that i have provided above http://boredzo.org/iso8601unparser/ – Satish Azad Mar 04 '14 at 06:14