ios 从字符串创建一个 NSDate
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9769190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
create a NSDate from a string
提问by samir
I have an NSString
like this: @"3/15/2012 9:15 PM"
and I would like to convert it to NSDate
, I have done like this:
我有一个NSString
这样的:@"3/15/2012 9:15 PM"
我想将它转换为NSDate
,我这样做了:
NSString *str =@"3/15/2012 9:15 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"mm/dd/yyyy HH:mm"];
NSDate *date = [formatter dateFromString:];
NSLog(@"%@", date); // date = null
Can you help me please, thanks.
你能帮我吗,谢谢。
回答by Ilanchezhian
Use the following solution
使用以下解决方案
NSString *str = @"3/15/2012 9:15 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"MM/dd/yyyy HH:mm a";
NSDate *date = [formatter dateFromString:str];
NSLog(@"%@", date);
Edit:Sorry, the format should be as follows:
编辑:对不起,格式应如下:
formatter.dateFormat = @"MM/dd/yyyy hh:mm a";
And the time shown will be GMT time. So if you add/subtract the timezone, it would be 9:15 PM.
显示的时间将是 GMT 时间。因此,如果您添加/减去时区,则为晚上 9:15。
Edit: #2
编辑:#2
Use as below. You would get exact time too.
如下使用。你也会得到准确的时间。
NSString *str = @"3/15/2012 9:15 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"MM/dd/yyyy hh:mm a";
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
formatter.timeZone = gmt;
NSDate *date = [formatter dateFromString:str];
NSLog(@"%@",date);
回答by samir
Your formatter is wrong. According to the NSDateFormatter documentation it should be "MM/dd/yyyy h:mm a". You also probably want to set the locale as stated in the Date Formatting Guideline:
你的格式化程序有问题。根据 NSDateFormatter 文档,它应该是“MM/dd/yyyy h:mm a”。您可能还想按照日期格式指南中的说明设置语言环境:
If you're working with fixed-format dates, you should first set the locale of the date formatter to something appropriate for your fixed format. In most cases the best locale to choose is en_US_POSIX, a locale that's specifically designed to yield US English results regardless of both user and system preferences.
如果您使用固定格式的日期,您应该首先将日期格式化程序的区域设置为适合您的固定格式的内容。在大多数情况下,要选择的最佳语言环境是 en_US_POSIX,这是一种专门设计用于产生美国英语结果而不管用户和系统偏好的语言环境。
Try this:
尝试这个:
NSString *str = @"3/15/2012 9:15 AM";
NSLocale *enUSPOSIXLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setLocale:enUSPOSIXLocale];
[formatter setDateFormat:@"MM/dd/yyyy h:mm a"];
NSDate *date = [formatter dateFromString:str];
NSLog(@"%@",date);
Outputs:
输出:
2012-03-19 12:37:27.531 Untitled[6554:707] 2012-03-15 08:15:00 +0000
回答by Damo
Hmmm - you've gone wrong for a couple of reasons
嗯 - 你有几个原因出错了
- the NSDateFormatter is strict and you have not been.
- you didn't supply str to dateFromString:
- NSDateFormatter 是严格的,而你没有。
- 你没有提供 str 到 dateFromString:
NSString* str = @"3/15/2012 9:15 PM";
NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"MM/dd/yyyy HH:mm a"];
NSDate* date = [formatter dateFromString:str];
NSLog(@"%@",date);
回答by Rama Rao
NSString *str =@"3/15/2012 9:15 PM";
NSDateFormatter *dateformatter = [[NSDateFormatter alloc]init];
[dateformatter setDateFormat:@"MM/dd/yyyy"];
NSArray *firstSplit = [str componentsSeparatedByString:@" "];
NSDate *dateSelected = [dateformatter dateFromString:[firstSplit objectAtIndex:0]];
[dateformatter setDateFormat:@"mm:ss"];
NSDate *dateSelected2 = [dateformatter dateFromString:[firstSplit objectAtIndex:1]];
NSLog(@"%@",[dateformatter stringFromDate:dateSelected2]);
NSLog(@"%@",[dateformatter stringFromDate:dateSelected]);