NSDateFormatter returns nil for #"dd-MM-yy" in iOS 3.0 - cocoa-touch

I have this part of code:
NSDate *date =nil;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateFormat:#"dd-MM-yy"];
date = [dateFormatter dateFromString:inString];
[dateFormatter release];
It works perfectly fine, as expected in iOS 4.0. But the same code doesnt in 3.0.
The string which I am getting, is like "12-Nov-10" and this is contained in inString pointer.
The date formatter returns nil if the native OS is 3.0 or 3.1. For some reasons I need to stick to the same date format. Has anyone else faced this problem? Any suggestions to resolve this issue?
Thanks,
Raj
Edit:
The proper code, after following suggestions pointed out by Harkonian and the Q&A discussions:
NSDate *date =nil;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateFormat:#"dd-MMM-yy"];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setLocale:enUSPOSIXLocale];
[enUSPOSIXLocale release];
date = [dateFormatter dateFromString:inString];
[dateFormatter release];

An extremely useful page does not exist anymore, so just putting its content here for archival purpose:
a: AM/PM
A: 0~86399999 (Millisecond of Day)
c/cc: 1~7 (Day of Week)
ccc: Sun/Mon/Tue/Wed/Thu/Fri/Sat
cccc: Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday
d: 1~31 (0 padded Day of Month)
D: 1~366 (0 padded Day of Year)
e: 1~7 (0 padded Day of Week)
E~EEE: Sun/Mon/Tue/Wed/Thu/Fri/Sat
EEEE: Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday
F: 1~5 (0 padded Week of Month, first day of week = Monday)
g: Julian Day Number (number of days since 4713 BC January 1)
G~GGG: BC/AD (Era Designator Abbreviated)
GGGG: Before Christ/Anno Domini
h: 1~12 (0 padded Hour (12hr))
H: 0~23 (0 padded Hour (24hr))
k: 1~24 (0 padded Hour (24hr)
K: 0~11 (0 padded Hour (12hr))
L/LL: 1~12 (0 padded Month)
LLL: Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec
LLLL: January/February/March/April/May/June/July/August/September/October/November/December
m: 0~59 (0 padded Minute)
M/MM: 1~12 (0 padded Month)
MMM: Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec
MMMM: January/February/March/April/May/June/July/August/September/October/November/December
q/qq: 1~4 (0 padded Quarter)
qqq: Q1/Q2/Q3/Q4
qqqq: 1st quarter/2nd quarter/3rd quarter/4th quarter
Q/QQ: 1~4 (0 padded Quarter)
QQQ: Q1/Q2/Q3/Q4
QQQQ: 1st quarter/2nd quarter/3rd quarter/4th quarter
s: 0~59 (0 padded Second)
S: (rounded Sub-Second)
u: (0 padded Year)
v~vvv: (General GMT Timezone Abbreviation)
vvvv: (General GMT Timezone Name)
w: 1~53 (0 padded Week of Year, 1st day of week = Sunday, NB: 1st week of year starts from the last Sunday of last year)
W: 1~5 (0 padded Week of Month, 1st day of week = Sunday)
y/yyyy: (Full Year)
yy/yyy: (2 Digits Year)
Y/YYYY: (Full Year, starting from the Sunday of the 1st week of year)
YY/YYY: (2 Digits Year, starting from the Sunday of the 1st week of year)
z~zzz: (Specific GMT Timezone Abbreviation)
zzzz: (Specific GMT Timezone Name)
Z: +0000 (RFC 822 Timezone)

If you're working with user-visible dates, you should avoid setting a date format string because it's very hard to predict how your format string will be expressed in all possible user configurations. Rather, you should try and limit yourself to setting date and time styles (via -[NSDateFormatter setDateStyle:] and -[NSDateFormatter setTimeStyle:]).
On the other hand, 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" is also invariant in time (if the US, at some point in the future, changes the way it formats dates, "en_US" will change to reflect the new behaviour, but "en_US_POSIX" will not), and between machines ("en_US_POSIX" works the same on iPhone OS as it does on Mac OS X, and as it it does on other platforms).
Once you've set "en_US_POSIX" as the locale of the date formatter, you can then set the date format string and the date formatter will behave consistently for all users.
The above info and more can be found in Apple's Technical Q&A QA1480
Here's a snippet of code from my app which implements the above recommendation :
static NSDateFormatter* dateFormatter = nil;
if (!dateFormatter) {
dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[[NSLocale alloc]
initWithLocaleIdentifier:#"en_US_POSIX"] autorelease];
assert(enUSPOSIXLocale != nil);
[dateFormatter setLocale:enUSPOSIXLocale];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
dateFormatter.dateFormat = #"EEE, dd MMM yyyy HH:mm:ss +0000";
}

Related

Why I can't get time this way

strTime=#"13:00";
NSDateFormatter * df = [[NSDateFormatter alloc]init];
[df setDateFormat:#"hh:mm"];
NSDate * date = [df dateFromString:strTime];
Everything seems fine.
Yet the content of date is nil.
Why?
What did I do wrong?
Also I changed to #"HH:mm"
NSDateFormatter * df = [[NSDateFormatter alloc]init];
[df setDateFormat:#"HH:mm"];
NSDate * date = [df dateFromString:strTime];
Result is the following:
(lldb) po strTime
13:00
(lldb) po date
2000-01-01 06:00:00 +0000
(lldb) po strTime
15:45
(lldb) po date
2000-01-01 08:45:00 +0000
I think I want something closer to today's date
I suppose what you did wrong is not read the documentation enough. But it's actually pretty obscure. The Data Formatting Guide points you to §8 Date Format Pattern, in Unicode Technical Standard #35, which says this about h:
Hour [1-12]. When used in skeleton data or in a skeleton passed in an API for flexible date pattern generation, it should match the 12-hour-cycle format preferred by the locale (h or K); it should not match a 24-hour-cycle format (H or k). Use hh for zero padding.
And says this about H:
Hour [0-23]. When used in skeleton data or in a skeleton passed in an API for flexible date pattern generation, it should match the 24-hour-cycle format preferred by the locale (H or k); it should not match a 12-hour-cycle format (h or K). Use HH for zero padding.
So you should try using HH:mm as your format:
[df setDateFormat:#"HH:mm"];

Converting NSString to NSDate adds one year in some cases

I have some issues converting an NSString to NSDate since the end of the year. The code have always worked great before, but it suddenly started to behave wierd...
For example 2013-01-05 becomes 2014-01-05 when converted to NSDate.
Since it's a whole year it doesn't feel like it's the timezone spooking.
It's not doing this with dates from 2012.
Does anybody have an idea of what might be wrong?
Code:
NSString *dateString = postInfo.noteDate;
NSString *newDateString = [dateString substringToIndex:10];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormat dateFromString:newDateString];
newDateString returns 2013-01-05
date returns 2014-01-05
From the docs:
Y 1..n 1997 Year (in "Week of Year" based calendars). Normally the length specifies the padding, but for two letters it also specifies the maximum length. This year designation is used in ISO year-week calendar as defined by ISO 8601, but can be used in non-Gregorian based calendar systems where week date processing is desired. May not always be the same value as calendar year.
y 1..n 1996 Year. Normally the length specifies the padding, but for two letters it also specifies the maximum length.
So you want 'yyyy'
This 'bug' is also discussed in the fantastic WWDC 2011 Video "Session 117 - Performing Calendar Calculations", a must-see for any iOS/Cocoa-Developer.
Wikipedia article on ISO 8601
NSDate *date = [[NSDate alloc] init];
date = [dateFormat dateFromString:newDateString];
You create a NSDate and than you create another and overwrite the first one. Just do
NSDate *date = [dateFormat dateFromString:newDateString];
use yyyy for year not YYYY, which gives week year. see the ISO standard.
Use yyyy in small letters, YYYY is another thing:
Year (in "Week of Year" based calendars). This year designation is used in ISO year-week calendar as defined by ISO 8601, but can be used in non-Gregorian based calendar systems where week date processing is desired. May not always be the same value as calendar year.
see http://www.unicode.org/reports/tr35/tr35-19.html#Date_Format_Patterns
I hope this will helps u. Try this
- (NSDate*) dateFromString:(NSString*)aStr
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease]];
[dateFormatter setDateFormat:#"MM/dd/yyyy HH:mm:ss a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSLog(#"%#", aStr);
NSDate *aDate = [dateFormatter dateFromString:aStr];
[dateFormatter release];
return aDate;
}

NSDateFormatter dateFromString coming back null

I am trying to get a string date into a date object so I can do some comparisons. My code is as follows:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy/MM/dd_hh:mm:ss"];
NSDate *date = [formatter dateFromString:#"2012/02/06_20:35:59"];
[formatter release];
NSLog(#"date = %#", date);
My NSLog is showing that *date = null;
Any help is appreciated. Thanks.
Does't your format need to be yyyy/MM/dd_HH:mm:ss?
hh is 0-12 where as HH is 0-23.
For reference, here is the doc where I got that nugget of information from - http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [formatter dateFromString:#"2012-02-06 20:35:59"];
[formatter release];
NSLog(#"date = %#", date);
d Day of the month as digits; no leading zero for single-digit days.
dd Day of the month as digits; leading zero for single-digit days.
ddd Day of the week as a three-letter abbreviation.
dddd Day of the week as its full name.
m Month as digits; no leading zero for single-digit months.
mm Month as digits; leading zero for single-digit months.
mmm Month as a three-letter abbreviation.
mmmm Month as its full name.
yy Year as last two digits; leading zero for years less than 10.
yyyy Year represented by four digits.
h Hours; no leading zero for single-digit hours (12-hour clock).
hh Hours; leading zero for single-digit hours (12-hour clock).
H Hours; no leading zero for single-digit hours (24-hour clock).
HH Hours; leading zero for single-digit hours (24-hour clock).
M Minutes; no leading zero for single-digit minutes.
Uppercase M unlike CF timeFormat‘s m to avoid conflict with months.
MM Minutes; leading zero for single-digit minutes.
Uppercase MM unlike CF timeFormat‘s mm to avoid conflict with months.
s Seconds; no leading zero for single-digit seconds.
ss Seconds; leading zero for single-digit seconds.

Discrepancy between SQLite's strftime / julianday and Cocoa's timeIntervalSince1970?

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *date1 = [dateFormatter dateFromString:#"1213-02-27"];
NSTimeInterval nsdateResult = [date1 timeIntervalSince1970];
NSTimeInterval sqliteResult = -23883638400;
GHAssertEquals(sqliteResult, nsdateResult, #"sqlite and nsdate should agree");
The expected number is what SQLite tells me should be the result, but this test fails since Cocoa tells me -23883033600 instead of -23883638400...i.e. 4 days later!
What gives? Am I doing something wrong or is either SQLite or Cocoa wrong?
Edit
Just checked the math and it seems Cocoa is giving me a wrong value: according to Wolfram Alpha, there are 276431 days between 2/27/1213 and 1/1/1970. Take this times 24*60*60, and you get the value given by SQLite... so am I doing something wrong or is this some kind of Cocoa bug?
Edit 2
Fvu is right, it has to do with the Gregorian/Julian craziness back in 1582.
NSDate *date1 = [dateFormatter dateFromString:#"1582-10-15"];
NSDate *date2 = [dateFormatter dateFromString:#"1582-10-14"];
NSTimeInterval i = [date2 timeIntervalSinceDate:date1];
Surprisingly, i is 777600, i.e. 10/14 is 9 days LATER than 10/15 because it automatically is viewed as a Julian date whereas 10/15 is viewed as a Gregorian date. So basically what I will need to do is convert any dates before 10/15 from the Julian to the Gregorian calendar.
Your sample date predates the Gregorian calendar so I think some oddities can be expected. However, as at the introduction of the Gregorian calendar the date advanced by 10 days I don't have a bulletproof explanation for the calculated 4 day difference.

NSDate formatter

I try to convert a string to NSDATE with no luck unfortunately.
Friday, October 22, 11:26:45 ET 2010
I know the options for formatting (http://sree.cc/objective-c/nsdate-format-string-in-objective-c) but i cant get it to work.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"???????"];
anyone?
Date Format Specifiers.
So you'd probably need something like:
[dateFormatter setDateFormat:#"eeee, MMMM dd, HH:mm:ss z yyyy"];
eeee - Local day of week spelled out
MMMM - Month spelled out
dd - day of month with no leading zeros
HH - hour of day (24 hour format)
mm - minutes of hour (with leading zero)
ss - seconds of minute (with leading zero)
z - timezone (short wall time)
yyyy - calendar year