1

我正在尝试格式化 JSON 响应日期值:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
    //Wed Dec 01 17:08:03 +0000 2010
    [df setDateFormat:@"eee, MMM dd HH:mm:ss ZZZZ yyyy"];
    NSDate *date = [df dateFromString:[twitter objectForKey:@"created_at"]];
    [df setDateFormat:@"dd/MM/yyyy"];
    NSString *dateStr = [df stringFromDate:date];

我遵循这个问题的说明:

iOS 中的日期/时间解析:如何处理(或不处理)时区?

实际上,JSON 响应中的日期值是:

created_at":"Mon, 28 May 2012 11:20:29 +0000"

接收值时日期变量为零[df dateFromString:[twitter objectForKey:@"created_at"]];

非常感谢

4

5 回答 5

6

如果月份是缩写的(Sept),请尝试这种格式:

[df setDateFormat:@"EEE, d MMM y HH:mm:ss Z"];;

[df setDateFormat:@"EEE, d MMMM y HH:mm:ss Z"];

如果写出月份名称(九月)。

格式说明符的完整列表。

我还必须更改语言环境

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
[df setDateFormat:@"EEE, d MMM y HH:mm:ss Z"];
NSDate *date = [df dateFromString:@"Mon, 28 May 2012 11:20:29 +0000"];

[df setDateFormat:@"dd/MM/yyyy"];
NSString *dateStr = [df stringFromDate:date];

NSLog(@"%@",dateStr);

输出

28/05/2012
于 2012-05-28T20:03:26.383 回答
5

我发现 twitter Json 响应中的“created_at”属性是这种格式:

Thu Apr 19 10:07:29 +0000 2012

所以我使用了这个格式化程序:

[df setDateFormat:@"EEE MMM d HH:mm:ss Z y"];

它工作正常;)

于 2013-01-09T15:52:49.290 回答
3

要验证您使用的格式,请从日期创建一个字符串:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"eee, MMM dd HH:mm:ss ZZZZ yyyy"];
NSLog(@"%@", [df stringFromDate: [NSDate date]];

将此与您的 JSON 响应中的日期进行比较,您应该能够看到您的格式错误的地方。


斯威夫特

let df = NSDateFormatter()
df.dateFormat = "eee, MMM dd HH:mm:ss ZZZZ yyyy"
let dateStr = df.stringFromDate(NSDate())
println("\(dateStr)")
于 2012-05-28T20:16:15.113 回答
0

正确的格式化程序值为:

[df setDateFormat:@"eee, dd MMM yyyy HH:mm:ss ZZZZ"];

为了:

created_at":"Mon, 28 May 2012 11:20:29 +0000"
于 2012-05-28T20:45:35.353 回答
0
"created_at" = "Sat Apr 05 06:31:57 +0000 2014";

以下方法返回 twit 出现之前的多少时间。

-(NSString*)retrivePostTime:(NSString*)postDate{

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
   [df setDateFormat:@"eee MMM dd HH:mm:ss ZZZZ yyyy"];
    NSDate *userPostDate = [df dateFromString:postDate];


    NSDate *currentDate = [NSDate date];
    NSTimeInterval distanceBetweenDates = [currentDate timeIntervalSinceDate:userPostDate];

    NSTimeInterval theTimeInterval = distanceBetweenDates;

    // Get the system calendar
    NSCalendar *sysCalendar = [NSCalendar currentCalendar];

    // Create the NSDates
    NSDate *date1 = [[NSDate alloc] init];
    NSDate *date2 = [[NSDate alloc] initWithTimeInterval:theTimeInterval sinceDate:date1];

    // Get conversion to months, days, hours, minutes
    unsigned int unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit | NSSecondCalendarUnit;

    NSDateComponents *conversionInfo = [sysCalendar components:unitFlags fromDate:date1  toDate:date2  options:0];

    NSString *returnDate;
    if ([conversionInfo month] > 0) {
        returnDate = [NSString stringWithFormat:@"%ldmth ago",(long)[conversionInfo month]];
    }else if ([conversionInfo day] > 0){
        returnDate = [NSString stringWithFormat:@"%ldd ago",(long)[conversionInfo day]];
    }else if ([conversionInfo hour]>0){
        returnDate = [NSString stringWithFormat:@"%ldh ago",(long)[conversionInfo hour]];
    }else if ([conversionInfo minute]>0){
        returnDate = [NSString stringWithFormat:@"%ldm ago",(long)[conversionInfo minute]];
    }else{
        returnDate = [NSString stringWithFormat:@"%lds ago",(long)[conversionInfo second]];
    }
    return returnDate;
}
于 2014-04-05T07:56:03.993 回答