简体   繁体   English

NSDate格式化程序将NSString转换为NSDate时出现null的问题

[英]NSDate formatter Problem its getting null while converting NSString to NSDate

hi to all I am Struggling to convert NSString to NSDate with the Help of formatter but its going to be null Can any one help please here is my code... 嗨,所有我正在努力将NSString转换为NSDate与格式化程序的帮助,但它将为null任何人可以帮助请在这里是我的代码...

 NSString *dateString=[NSString stringWithFormat:@"10/26/2010/09:56:56 PM"];
 NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
 [dateFormat setDateFormat:@"mm/dd/yyyy HH:mm:ss a"];
 NSDate *localDateTime =[dateFormat dateFromString:dateString];
 NSLog(@"client date is %@",localDateTime);

Thx in Advance Thx提前

Could be because your dateString and format does not match. 可能是因为你的dateString和格式不匹配。

10/26/2010/09:56:56 PM

mm/dd/yyyy HH:mm:ss a

Do you see the difference? 你看得到差别吗? :) They actually have to match. :)他们实际上必须匹配。 In the format you have a space between the year and hours and in the actual date there is a slash. 在格式中,您在年份和小时之间有一个空格,在实际日期中有一个斜杠。 Month you match by 'mm' which is actually minutes and should be 'MM'. 你匹配的月份是'mm',实际上是分钟,应该是'MM'。 Change to: 改成:

10/26/2010 09:56:56 PM

MM/dd/yyyy HH:mm:ss a

There might be more problems so I suggest you actually look at the guide to get it right. 可能会有更多问题,所以我建议您实际查看指南以使其正确。

 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd 'at' HH:mm"];

NSDate *formatterDate = [dateFormatter dateFromString:@"1999-07-11 at 10:30"];
[dateFormatter release];

Try this out. 试试吧。

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

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