Date format string for a date string - objective-c

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

Related

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.

How to convert NSString to NSDate?

I want to search my Array of Dictionary for particular date.Ex I want to search my array of dictionary for date "16 Jan 2012" which is in string format but my dictionary item in array contains date and time, say "16 Jan 2012 somehours:somemins:somesecs".I am converting string format date in NSDate format but I am getting date as 2012-01-15 18:30:00 +0000 instead of 2012-01-16.I am using NSPredicate to search for the date which convert date into seconds as follows "Date == CAST(348345000.000000, "NSDate")" and compare so even though my records contain date as "16 Jan 2012 somehours:somemins:somesecs" it will not satisfied the criteria.I want that the records/array containing date as "16 Jan 2012 somehours:somemins:somesecs" should satisfied the search criteria.Please can anyone know how to achieve this?
Thanks in advance!
To rid yourself of NSDateFormatter's automatic adjustment for your time zone use something like this.
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
To elaborate; "16 Jan 2012" Contains no hour, minute, or seconds value, so when parsing it with an NSDateFormatter one would expect an NSDate with a description of "2012-01-16 00:00:00 +000" But that's not what you're getting because the date formatter is adjusting for the 5:30 Hours between GMT and India?(Assuming based on :30 differential). By setting the date formatter's time zone explicitly you avoid this problem.
You need to use NSDateFormatters to convert the date string from one format to another. You can have an inputDateFormatter where you use dateFromString to give you an NSDate, and then feed this into an outputDateFormatter which gives your desired string.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMM yyyy"];
NSString *dateInFormatYouWant = [dateFormatter stringFromDate:yourDate]
I'm not sure if there is a more efficient way of doing it than this.

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

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