开发者

iPhone:How to convert Regular Expression into Date Format?

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.


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜