开发者

nil result from NSDateFormatter with 0's in format string

开发者 https://www.devze.com 2023-03-16 02:28 出处:网络
I\'m getting a date from a 开发者_高级运维webservice back in the form MM00yyyy -- it is just the two-digit month, followed by two 0\'s, and then the four-digit year.When I do this:

I'm getting a date from a 开发者_高级运维webservice back in the form MM00yyyy -- it is just the two-digit month, followed by two 0's, and then the four-digit year. When I do this:

NSString *expDate = @"12001975";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM00yyyy"];
NSDate postDate = [dateFormat dateFromString:expDate];

[dateFormat dateFromString] returns nil for some reason. I have also tried MMddyyyy, and MM'0''0'yyyy, with no success either way. I am converting a similar date, except the 0's are actually the day with no problem using the same method.


To get this working, I would just use the following pattern MMHHyyyy. Since you need only the date and not neccessarily the hour, the HH will use the 00 to set the time as zeroth hour and hence you will get the date that you are looking for. Again this is just a hack and a workaround only to solve your current problem.


Have a look at the Date Formatting Guide from Apple. The section "Use Format Strings to Specify Custom Formats" lists all the different standards the are supported by various iOS versions for specifying a format string. I would say that "00" is not allowed, so that is the reason why "MM00yyyy" is failing. Similarly, "MMddyyyy" is also failing because no day can be "00".

I don't know if you can have more luck with UNIX functions, as the Apple doc suggests:

For date and times in a fixed, unlocalized format, that are always guaranteed to use the same calendar, it may sometimes be easier and more efficient to use the standard C library functions strptime_l and strftime_l.

Be aware that the C library also has the idea of a current locale. To guarantee a fixed date format, you should pass NULL as the loc parameter of these routines. This causes them to use the POSIX locale (also known as the C locale), which is equivalent to Cocoa's en_US_POSIX locale, as illustrated in this example.

 struct tm  sometime;
 const char *formatString = "%Y-%m-%d %H:%M:%S %z";
 (void) strptime_l("2005-07-01 12:00:00 -0700", formatString, &sometime, NULL);
 NSLog(@"NSDate is %@", [NSDate dateWithTimeIntervalSince1970: mktime(&sometime)]);
 // Output: NSDate is 2005-07-01 12:00:00 -0700


Getting the format strings right seems much more like art than science. I suggest you make a new string without the 00 in it and then have your DateFromatter process that with "MMyyyy".

While this might not be the "correct" way to do it, it should solve your problem pretty quickly.


The zeros are unsupported symbols. Apple supports the following characters for date formatting: http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns See the day section.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号