iPhone:如何将正则表达式转换为日期格式? [英] iPhone:How to convert Regular Expression into Date Format?

查看:74
本文介绍了iPhone:如何将正则表达式转换为日期格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友

我有一个类似"/Date(1312628727617 + 0000)/"的字符串,并且该正则表达式在iPhone中转换为Date格式,因此请提供任何链接或任何想法来开发此功能.

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.

谢谢.

推荐答案

使用在方法下面,然后将您的正则表达式日期/Date(1312628727617 + 0000)/作为参数传递.

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];
}

  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.

这篇关于iPhone:如何将正则表达式转换为日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆