将日期转换为正确的格式 [英] converting date to correct format

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

问题描述

我有一个Web服务,可以通过以下方式回传我的日期.

I've a webservice which gives back my date in the following way.

Wed Oct 31 11:59:44 +0000 2012

但是我希望它以这种方式将其还给我

But I want it to give it back in this way

31-10-2012 11:59

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

I know that it should be done with a NSDateFormatter. But I don't now how to implement it in the correct way.

我有这样的东西.

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对象

Step 1: create an NSDateFormatter to convert your string from server to an NSDate object by setting the format to the format of the "server string"

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对象转换为字符串对象

Step 2: create another NSDateFormatter with the desired output string and convert your new NSDate object to a string object using the new NSDateFormatter

NSDateFormatter *f2 = [[NSDateFormatter alloc] init];
[f2 setDateFormat:@"dd-MM-y hh:mm"];
[f2 setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSString *s = [f2 stringFromDate:date];

desiredformat = s;

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

P.S. I'm not sure of f format, check this link http://www.developers-life.com/nsdateformatter-and-uifont.html

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

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