NSDate formatter - objective-c

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

Related

Date format string for a date string

In Object-C, how about the date format string for the following date string:
2017:04:11 17午後5:40:00
I have tried "yyyy:MM:dd HH:mm:ss a" and set Locale is currentLocale.
My device is in Japanese language, 24hour display is OFF.
Just take a look at all the parts of the string you want (I hope I got them correctly - otherwise just look up the correct format characters in the link below):
2017 year with four digits: yyyy
04 month with two digits: MM
11 day with two digits: dd
17 hour in 24h hour format with two digits: HH
午後 am/pm string: a
5 hour in 12h format with one or two digits: h
40 minute with 2 digits: mm
00 seconds with 2 digits: ss
Combined: yyyy:MM:dd HH a h:mm:ss
For a reference of the strings (OS X >= v10.9 and iOS >= 7) see Unicode standard 35 (for links for older versions see Appels Data Formatting Guide)
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy:MM:dd HH a h:mm:ss"];
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:513650400];
NSLog(#"%#", [dateFormatter stringFromDate:date]);
// Output: 2017:04:11 17 午後 5:40:00

Converting NSDate

I have an NSDate (lets call it x), 12 September, 2012 10:18PM (GMT). I want to convert x to a minute before my current time zone's (EST) midnight. So, x represented in EST with NSDateFormatter after conversion would be 12 September, 2012 11:59PM (EST). What's the best way to do this?
Thanks
Take a look at NSDateComponents: https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSDateComponents_Class/Reference/Reference.html
I believe you'll need to convert the NSDate to NSDateComponents, set the time to 11:59PM, then convert back to NSDate.
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:(NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:date];
[components setHour:23];
[components setMinute:59];
NSDate *convertedDate = [calendar dateFromComponents:components];
NSLog(#"date=%#, convertedDate=%#", date, convertedDate);
Such manipulations can be easily done with the numeric representation of a date. In this representation, you deal with the number of seconds since a reference date.
The reference date for the purpose of the timeIntervalSinceReferenceDate is January 1st, 2001, at 00:00:00 GMT.
NSDate* date = [NSDate date];
NSInteger secondsSinceReferenceDate = [date timeIntervalSinceReferenceDate];
secondsSinceReferenceDate += 86400 - (secondsSinceReferenceDate % 86400);
secondsSinceReferenceDate -= 60;
secondsSinceReferenceDate -= [NSTimeZone.localTimeZone secondsFromGMTForDate:date];
NSDate* justBeforeToday =
[NSDate dateWithTimeIntervalSinceReferenceDate:secondsSinceReferenceDate];
NSLog(#"Date used was %#", date);
NSLog(#"Just before tomorrow is %#", justBeforeToday);
Since there are 86400 seconds in a day (24 hours times 60 minutes 60 times 60 seconds = 86400 seconds), you know that 86400 - (secondsSinceReferenceDate % 86400) is the number of seconds there are still to midnight. So if you take today's date (or any other valid date), add this number of seconds, and then subtract another 60 seconds, you'll have today's evening at 11:59 PM in the GMT timezone.
With [NSTimeZone.localTimeZone secondsFromGMTForDate:], you know how many seconds your timezone is offset from the GMT timezone. By subtracting this offset to your integer representation, you effectively get when it will be 11:59 PM in your local timezone.
Here's a sample output:
Date used was 2012-09-12 22:37:49 +0000
Just before tomorrow is 2012-09-13 03:59:00 +0000
I'm in the EDT timezone too, and this looks like the correct answer (remember Standard Time is -5 from GMT, but right now we're in daylight savings, so it's -4 from GMT, which is invariant).

Using NSDateFormatter on iOS

I am currently using an NSDateFormatter on my application to show the date in the format that i want. My NSDateFormatter looks like this : [formatter setDateFormat:#"MMM dd, yyyy hh:mm"]; which would give me dates like : Sep 02 , 2012 08:30 .
I have 2 questions. i want to show the time in military time how can i do that with the formatter? I know how to show the AM/PM but i would like the time above to look 20:30. Secondly how can i make the month appear as a number? September would be 09 .
This site has a nice table with the format specifiers you can use, follow up from that.
In this case, you should use [formatter setDateFormat:#"MM dd, yyyy HH:mm"]; To get 09 02, 2012 20:30
Use capital H for the hour and only two M's for the month.
[formatter setDateFormat:#"MM dd, yyyy HH:mm"];

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.

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

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";
}