通过 NSDateFormatter 从未知格式的字符串获取 NSDate
我知道这里有很多 NSDateFormatter
问题,所以如果我重复,我很抱歉。我只是找不到任何符合我要求的东西。
从这里的所有问题中,我得出的结论是,如果您的格式化程序对象没有正确的日期, -[NSDateFormatter dateFromString:]
将始终返回 NULL
格式。如果不知道格式,如何从字符串中获取日期?我正在尝试从 UITextField
获取日期。
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
[formatter setLenient:YES];
NSDate *tempDate = [formatter dateFromString:self.birthdayTxtfld.text];
self.currentCustomer.birthday = ([self.birthdayTxtfld.text isEqualToString:@""]) ? NULL : tempDate;
[formatter release];
tempDate
始终为NULL
。
I know there are many NSDateFormatter
questions on here, so if I duplicate, I'm sorry. I just couldn't find anything that was quite what Im asking.
From all the questions here on SO, I have come to the conclusion that -[NSDateFormatter dateFromString:]
will always return NULL
if your formatter object doesn't have the correct date format. How do you get a date from a string if you don't know the format? I'm trying to get a date from a UITextField
.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
[formatter setLenient:YES];
NSDate *tempDate = [formatter dateFromString:self.birthdayTxtfld.text];
self.currentCustomer.birthday = ([self.birthdayTxtfld.text isEqualToString:@""]) ? NULL : tempDate;
[formatter release];
tempDate
is always NULL
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你采取了错误的方法。另一方面,我会限制和格式化 UITextField,以便用户必须以特定格式输入日期。或者只使用日期选择器。用户可以给您提供多种不同的输入。
或者您可以阅读以下内容: NSDate
另一个选项是创建可接受的日期格式列表:
#define DATEFORMATS @[@"MM/dd/yyyy", @"MM/dd/yy",...
然后有一个方法,您可以将日期字符串传递给并检查是否可以格式化它:
如果您得到nil,则它不是日期或其格式不支持。否则您将获得所需的正确格式。您可以对此进行切换以返回日期而不是格式。我这样做是因为我需要项目的格式而不是日期。
I think your taking the wrong approach. I would on the other hand restrict and format the UITextField so the user has to enter the date in a specific format. Or just use a date picker. There are just way too may different inputs the user could give you.
Or you can read through this: NSDate
Another option is to create a list of accepted date formats:
#define DATEFORMATS @[@"MM/dd/yyyy", @"MM/dd/yy",...
Then Have a method that you pass the date string to and check if you can format it:
If you get nil its not a date or its in a format you don't support. Otherwise you will have the correct format you need. You can switch this around to return the date instead of the format. I have it this way because I needed the format not the date for a project.
虽然我同意 @Jaybit 的观点,即您可能需要放弃文本框并使用更好的输入,但这个特定问题的答案在于一些巧妙的字符串解析。每当您进行字符串解析时,RegEx 都是您的朋友。 Web 开发人员最终不得不一直做这些废话。此示例使用 JavaScript,但 RegEx 应该具有足够的可移植性,以便可以在 ObjC 中工作:
http://www.codingoptimist.com/2009/07/using-javascript-and-regex-to-parse.html
您可以使用 RegExKit 或 NSRegularExpression
While I'm in agreement with @Jaybit that you probably need to ditch the text box and use a better input, the answer to this specific question lies in some crafty string parsing. Whenever you are doing string parsing, RegEx is your friend. Web developers end up having to do this crap all the time. This example is in JavaScript, but the RegEx ought to be portable enough that it works in ObjC:
http://www.codingoptimist.com/2009/07/using-javascript-and-regex-to-parse.html
You can do this with RegExKit or NSRegularExpression