将日期转换为正确的格式

2023-12-01

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

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

网络服务布局

"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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将日期转换为正确的格式 的相关文章

随机推荐