简体   繁体   English

NSDateFormatter在调用datefromstring方法时返回nil?

[英]NSDateFormatter returns nil while calling datefromstring method?

I'm trying to create a date object with the specified formatter but date formatter_datefromstring method returns nil. 我正在尝试使用指定的格式化程序创建日期对象,但date formatter_datefromstring方法返回nil。 Please let me know with the clear documentation samples. 请让我知道清晰的文档样本。 The String am trying to parse is "2:00 AM PDT on September 24, 2017". 我要解析的String是“ 2017年9月24日太平洋标准时间凌晨2:00”。 Thanks in advance 提前致谢

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
    [dateFormat setLocale:[NSLocale currentLocale]];
    [dateFormat setDateFormat:@"h:mm a Z 'on' MMMM d, yyyy"];
    NSLog(@"dateStr:==============> %@", dateStr);
    NSDate *date = [dateFormat dateFromString:dateStr];
    NSLog(@"Date:------------->%@", date);
    return date;

You are using the wrong timezone specifier. 您使用了错误的时区说明符。 Z is for timezones such as -0800 . Z用于诸如-0800时区。 You need z for short timezone abbreviations like PDT . 对于短时区缩写(例如PDT您需要z

Also, there is no reason to set the formatter's local to currentLocale and the timezone to systemTimeZone since those are the defaults. 同样,没有理由将格式化程序的本地设置为currentLocale ,将时区设置为systemTimeZone因为这些是默认设置。 And the timezone of the formatter is irrelevant when the string you are parsing contains timezone information. 当您要分析的字符串包含时区信息时,格式化程序的时区就无关紧要。

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"h:mm a z 'on' MMMM d, yyyy"];
NSLog(@"dateStr:==============> %@", dateStr);
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(@"Date:------------->%@", date);
return date;

However, since you are parsing a fixed format date string that is in English, you really should set the formatter's locale to the special locale of en_US_POSIX . 但是,由于您要解析英语的固定格式日期字符串,因此,您确实应该将格式程序的语言环境设置为en_US_POSIX的特殊语言环境。 This will ensure it handles the English month name no matter the user's locale. 这将确保无论用户的语言环境如何,都可以处理英语月份的名称。

[dateFormat setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]];

Can you check your dateStr date format and your given format same or not. 您可以检查dateStr日期格式和给定格式是否相同。 If both are not same format you will get nil object. 如果两者的格式都不相同,则会得到nil对象。 Try dateStr format in given below example. 在下面的示例中尝试使用dateStr格式。

NSString *dateStr = @"10:23 am Z on September 30, 2017";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormat setLocale:[NSLocale currentLocale]];
[dateFormat setDateFormat:@"h:mm a Z 'on' MMMM d, yyyy"];
NSLog(@"dateStr:==============> %@", dateStr);
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(@"Date:------------->%@", date);

In console you will get 2017-09-23 09:54:04.654597+0530 Date[4068:79926] dateStr:==============> 10:23 am Z on September 30, 2017 2017-09-23 09:54:04.657359+0530 Date[4068:79926] Date:------------->Sat Sep 30 15:53:00 2017 在控制台中,您将获得2017年9月30日上午10:23,2017年9月23日09:54:04.654597 + 0530 Date [4068:79926] dateStr:==============> 2017-09-23 09:54:04.657359 + 0530 Date [4068:79926] Date:-------------> Sat Sep 30 15:53:00 2017

Use the below locale the avoid returning nil value. 使用以下语言环境避免返回nil值。

NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];

Vote my code if it is usefull 如果有用,请投票给我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM