我有一个网络服务,可以通过以下方式返回我的日期。

Wed Oct 31 11:59:44 +0000 2012

但我希望它以这种方式还给我
31-10-2012 11:59

我知道应该用NSDateFormatter完成。但是我现在不知道如何以正确的方式实现它。

我有这样的东西。
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"dd/MM/yyyy"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT+0:00"]];
    NSDate *date = [dateFormatter dateFromString:[genkInfo objectForKey:DATE]];

有谁能够帮助我?

亲切的问候。

目前的代码
  NSDateFormatter *f = [[NSDateFormatter alloc] init];
    [f setDateFormat:@"E MMM d hh:mm:ss Z y"];
    NSDate *date = [f dateFromString:@"Wed Oct 31 11:59:44 +0000 2012"];
    NSDateFormatter *f2 = [[NSDateFormatter alloc] init];
    [f2 setDateFormat:@"dd-MM-y hh:mm"];
    [f2 setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    NSString *date2 = [f2 stringFromDate:date];

Web服务布局
"text": "KRC Genk | Zaterdag is er opnieuw een open stadiontour http://t.co/tSbZ2fYG",
"created_at": "Fri Nov 02 12:49:34 +0000 2012"

最佳答案

步骤1:通过将格式设置为“服务器字符串”的格式,创建NSDateFormatter将字符串从服务器转换为NSDate对象

NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"E MMM d hh:mm:ss Z y"];
NSDate *date = [f dateFromString:@"Wed Oct 31 11:59:44 +0000 2012"];

步骤2:使用所需的输出字符串创建另一个NSDateFormatter,然后使用新的NSDateFormatter将新的NSDate对象转换为字符串对象
NSDateFormatter *f2 = [[NSDateFormatter alloc] init];
[f2 setDateFormat:@"dd-MM-y hh:mm"];
[f2 setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSString *s = [f2 stringFromDate:date];

desiredformat = s;

附言我不确定f格式,请检查此链接
http://www.developers-life.com/nsdateformatter-and-uifont.html

关于objective-c - 将日期转换为正确的格式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13196353/

10-14 13:41
查看更多