NSDateFormatter dateFromString 转换 [英] NSDateFormatter dateFromString conversion

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

问题描述

我在将 NSString 对象转换为 NSDate 时遇到问题.这是我想要做的:我正在从 Internet 下载 RSS 消息.我有 NSXMLParser 解析的整个消息.在那之后,我有像 , 或保存到特定 NSStrings 的部分.我想将元素(包括 RSS 消息的发布日期)转换为 NSDate,以便我可以对其执行一些操作,例如对日期对象(例如排序、在时钟上显示等).这是我的样子:

I am having problems with conversion of NSString object to NSDate. Here is what I want to do: I am downloading an RSS Message from the internet. I have the whole message parsed by NSXMLParser. After that, I have parts like , or saved to particular NSStrings. I want to convert element (that includes publication date of RSS Message) to NSDate so that I could perform some operations on it like on a date object (e.g. sorting, showing on a clock etc.). Here is the way my looks like:

格林威治标准时间 2013 年 9 月 25 日星期三 12:56:57"

我尝试以这种方式将其转换为 NSDate:

I tried to convert it to NSDate in this way:

*//theString is NSString containing my date as a text
NSDate *dateNS = [[NSDate alloc] init];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"EEE, dd MMM yyyy hh:mm:ss ZZZ"];
dateNS = [dateFormatter dateFromString:theString];*

但是,在执行上述代码后,dateNS 始终显示为 (null).

However, after doing above code, dateNS always appear to be (null).

我的问题很简单:将具有这样格式的日期的 NSString 转换为 NSDate 对象的正确方法是什么?

My question is simple: what is the right way to convert NSString with date formatted like this to NSDate object?

顺便说一下,我看过网站http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns似乎有很多方法可以格式化特定日期,但我找不到我做错了什么.

By the way, I have seen the website http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns It seems that there are many ways to format particular date, but I could not find what I am doing wrong.

推荐答案

您的问题是您的日期格式化程序与您的日期字符串不同:您应该将日期格式化程序设置为与日期字符串相同的格式

Your problem is the your date formatter has not identical fort as your date string: You should set date formatter the same format like your date string

我的例子:

//将字符串转换为日期

// Convert string to date

    NSString *beginString = @"Sat, 30 Dec 2013 14:45:00 EEST";
    //beginString = [beginString stringByReplacingOccurrencesOfString:@"EEST" withString:@""];
    //beginString = [beginString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"]];
    [dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"Europe/Helsinki"]];
    [dateFormat setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss z"];
    dateFromString = [dateFormat dateFromString:beginString];

    //NSLog(@"Begin string: %@", beginString);

    //NSLog(@"not formated: %@", dateFromString);

    // Convert Date to string

    [dateFormat setTimeZone:[NSTimeZone localTimeZone]];
    [dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"ru_RU"]];
    [dateFormat setDateFormat:@"dd MMMM yyyy"];
    myStrDate = [dateFormat stringFromDate:dateFromString];
    [currentTitle setPubDate:myStrDate];

这篇关于NSDateFormatter dateFromString 转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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