字符串值不转换为日期格式 [英] String value not converting to date format
本文介绍了字符串值不转换为日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
字符串值不转换为日期格式。请参阅下面的代码...
{
NSString * TempDate = [NSString stringWithFormat:@%@%@ %@,Datelbl.text,yearString,TimeLbl.text]; // result is Thursday,November 21 2013 3:05 PM
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:[NSDateFormatter dateFormatFromTemplate:@EEEE,MMMM d yyyy hh:mm aoptions:0 locale:[NSLocale currentLocale]]];
AppointmentDate = [dateFormatter dateFromString:TempDate]; // in .h file NSDate * AppointmentDate;
}
我的问题是appointmentDate显示nil值?为什么?
$ p>
[dateFormatter setDateFormat:@EEEE','MMMM d yyyy hh:mm a];
您需要转义不是格式(逗号)一部分的字符。
我试过,现在工作了:
NSString * date = @ ,November 21 2013 3:05 PM;
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@EEEE','MMMM d yyyy hh:mm a];
NSDate * AppointmentDate = [dateFormatter dateFromString:date];
如果您的语言环境不是英语,请添加:
NSLocale * usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@en_US];
[dateFormatter setLocale:usLocale];
String value not converting to date format. see my code below...
{
NSString * TempDate = [NSString stringWithFormat:@"%@ %@ %@",Datelbl.text,yearString,TimeLbl.text]; // result is Thursday, November 21 2013 3:05 PM
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat : [NSDateFormatter dateFormatFromTemplate:@"EEEE, MMMM d yyyy hh:mm a" options:0 locale:[NSLocale currentLocale]]];
AppointmentDate = [dateFormatter dateFromString:TempDate];// in .h file NSDate *AppointmentDate;
}
My problem is appointmentDate displaying nil value?? why ? whats my mistake!!!
解决方案
Try this format:
[dateFormatter setDateFormat :@"EEEE',' MMMM d yyyy hh:mm a"];
You need to escape the character that is not part of the format (the comma).
I tried and it works now:
NSString *date = @"Thursday, November 21 2013 3:05 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat :@"EEEE',' MMMM d yyyy hh:mm a"];
NSDate *AppointmentDate = [dateFormatter dateFromString:date];
If your locale is not english, add this:
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[dateFormatter setLocale:usLocale];
这篇关于字符串值不转换为日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文