带有格式对话问题的NSString到NSDate

时间:2014-07-07 07:43:56

标签: ios objective-c datetime nsdate nsdateformatter

我已经在StackOverflow.com上搜索了这个问题的答案,但仍未取得任何成功。已经看过这里了:

iOS - Converting time and date to user time zone

Date Format - time zone

Get current iPhone device timezone date and time from UTC-5 timezone date and time iPhone app?

IOS how to set date format

这些都没有。


所以我有这个NSString日期格式:2014-05-07T10:28:52.000Z尝试将其转换为NSDate,这就是我正在使用的:

+ (NSDate*)stringToDate:(NSString*)string format:(NSString*)format
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:format];
    return [dateFormatter dateFromString:string];
}

这就是我使用它的方式:

NSDate *date = [AppUtils stringToDate:youtube.postDate format:@"yyyy-MM-dd'T'HH:mm:ssZZZ"];

我也尝试过这些格式:

yyyy-MM-dd'T'HH:mm:ss.ZZZ
yyyy-MM-dd'T'HH:mm.ss.ZZZ
yyyy-MM-dd'T'HH:mm.ssZZZ

如何成功将其转换为NSDate?

提前致谢!

2 个答案:

答案 0 :(得分:2)

如果日期格式是固定的,我的工作如下。

Replace T by space
Replace Z by blank 

然后进行格式化......

NSString *dateReceivedInString = @"2014-05-07T10:28:52.000Z";
dateReceivedInString = [dateReceivedInString stringByReplacingOccurrencesOfString:@"T" withString:@" "];
dateReceivedInString = [dateReceivedInString stringByReplacingOccurrencesOfString:@"Z" withString:@""];

现在使用

进行格式化
yyyy-MM-dd HH:mm:ss

编辑1

如果您想使用您的案例,请使用以下

yyyy-MM-dd'T'HH:mm:ss.SSSZ

编辑2

我试过这个并且它正在运作。

NSString *dateReceivedInString = @"2014-05-07T10:28:52.000Z";
dateReceivedInString = [dateReceivedInString stringByReplacingOccurrencesOfString:@"T" withString:@" "];
dateReceivedInString = [dateReceivedInString stringByReplacingOccurrencesOfString:@"Z" withString:@""];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
NSLog(@"ddddd====%@", [dateFormatter dateFromString:dateReceivedInString]);

编辑3

要使用下面的案例,请使用

NSString *dateReceivedInString = @"2014-05-07T10:28:52.000Z";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSLog(@"ddddd====%@", [dateFormatter dateFromString:dateReceivedInString]);

答案 1 :(得分:0)

以下是我用于将RFC3339 Date string转换为NSDate的两种方法,
以及将NSDate转换为RFC3339 Date string

的方法

将RFC3339日期字符串转换为NSDate的方法

+ (NSDate *)dateForRFC3339DateTimeString:(NSString *)rfc3339DateTimeString
{    
    NSDateFormatter *rfc3339DateFormatter = [[NSDateFormatter alloc] init];

    [rfc3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'"];
    [rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

    // Convert the RFC 3339 date time string to an NSDate.
    return [rfc3339DateFormatter dateFromString:rfc3339DateTimeString];}

将NSDate转换为RFC3339日期字符串的方法

+ (NSString *)RFC3339DateTimeFromDate:(NSDate *)aDate
{
    NSDateFormatter *rfc3339DateFormatter = [[NSDateFormatter alloc] init];

    [rfc3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'"];
    [rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

    return [rfc3339DateFormatter stringFromDate:aDate];
}