2

Hello friends,

I have a string like "/Date(1312628727617+0000)/" and this regular expression convert into Date format in iPhone so please provide any link or any idea to develop this functionality.

Thanks in advance.

Akshay
  • 5,747
  • 3
  • 23
  • 35
Nikunj Jadav
  • 3,417
  • 7
  • 38
  • 54

3 Answers3

6

Use the below method and pass your regular expression date /Date(1312628727617+0000)/ as parameter.

- (NSDate*) getDateFromJSON:(NSString *)dateString
{
    // Expect date in this format "/Date(1268123281843)/"
    int startPos = [dateString rangeOfString:@"("].location+1;
    int endPos = [dateString rangeOfString:@")"].location;
    NSRange range = NSMakeRange(startPos,endPos-startPos);
    unsigned long long milliseconds = [[dateString substringWithRange:range] longLongValue];
    NSLog(@"%llu",milliseconds);
    NSTimeInterval interval = milliseconds/1000;
    return [NSDate dateWithTimeIntervalSince1970:interval];
}

EDIT:

  NSDateFormatter *dateForm = [[NSDateFormatter alloc] init];
  [dateForm setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
  NSString *dateStr = [dateForm stringFromDate:<YourNSDateObject>];
  [dateForm setDateFormat:@"<Your Desired Date Format>"];
  NSDate *desireddate = [dateForm dateFromString:dateStr];

Hope this helps you.

Community
  • 1
  • 1
Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
  • Thanks a lot I used "/Date(1312628727617+0000)/" so its return 2011-08-06 16:35:27 +0530 but I want to display only 2011-08-06 16:35:27 so please help me – Nikunj Jadav Aug 11 '11 at 06:02
  • Then you can use NSDateFormatter and convert it to other date types but setting `setDateFormat:` of NSDateFormatter and then passing your NSDate object will give you the NSDate in desired format. Hope this is it. – Parth Bhatt Aug 11 '11 at 06:07
  • hello parth How can i pass NSDate object in NSDateFormatter?? please help – Nikunj Jadav Aug 11 '11 at 06:26
  • @Nikunj Jadav: Please check the **EDIT** Section of my answer – Parth Bhatt Aug 11 '11 at 10:40
  • 1
    You should probably give credit to toxaq for originally posting your first block of code: http://stackoverflow.com/questions/1757303/parsing-json-dates-on-iphone/2414159#2414159 – Brad Larson Aug 17 '11 at 14:51
  • @Brad Larson: Yeah you are right. I just copied this block of code from where I used it in my project. And also just to make sure that the Nikunj Jadav gets a precise answer for a trivial problem and can work faster into it. No bad intensions :) – Parth Bhatt Aug 17 '11 at 18:48
  • I've added a link back to the original answer for you. – Tim Post Aug 18 '11 at 10:22
0

Have a look at dateFromString: at http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html. You will find the many formats supported.

Akshay
  • 5,747
  • 3
  • 23
  • 35
  • I suppose `dateFromString` would work only if the date is NSString but in Native date format Like "12 July 2011" or may be "07-12-2011" or something similar. This function `dateFromString` would not work for date in the format `"/Date(1312628727617+0000)/"` – Parth Bhatt Aug 11 '11 at 05:54
  • Agreed. I got the impression that the string used in the question was just an example & the actual strings may vary. – Akshay Aug 11 '11 at 06:03
0

See this: - (NSDate *)dateFromString:(NSString *)string @ below link

http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html#//apple_ref/doc/c_ref/NSDate

viral
  • 4,168
  • 5
  • 43
  • 68