iOS - Get local date & time in International format and English language - ios

I am trying to get the user's local Date & Time using the below code
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"dd MMM yyyy"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
[timeFormatter setDateFormat: #"HH:mm"];
[timeFormatter setTimeZone:[NSTimeZone localTimeZone]];
self.appearedOnDate = [dateFormatter stringFromDate:[NSDate date]];
self.appearedOnTime = [timeFormatter stringFromDate:[NSDate date]];
It returns correct values, but in the language set on users device.
For example, in a case where user has Simplified Chinese set on his device, I am getting the output as
24 3月 2015 --- 22:13
How can I get the local time in English language ?
I guess this happens only in iOS8 and not on iOS6, but I am not sure.

You might add the following code before print your output.
[NSLocale availableLocaleIdentifiers];
NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[timeFormatter setLocale:locale];

Related

Unexpected behavior with NSDateFormatter on iOS 9.1

I have this code:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm Z"];
[dateFormat setTimeZone:[NSTimeZone localTimeZone]];
[dateFormat setLocale:[NSLocale currentLocale]];
NSString *sDate = [dateFormat stringFromDate:_datePicker];
_datePicker comes from this and datePicker is a UIDatePicker
_datePicker = datePicker.date;
This was working ok in iOS 7 fore sure, and now I'm testing in 9.1
So if the phone settings is set to show the time in the 24h format its ok, but if I change the settings to none 24h format I have a estrange behavior:
if is set to 24h the output is:
But if i change it to non 24h is:
As you can see is like is combining the 2 formats it adds for example the 13 of the 1 pm to the hour 131:00 or at 6 pm will be 186:00.
Is there a work around to always get the time in the 24h format?
Thanks in advance.
Hi I have sean this approach in another page and I have change the code to this:
NSDateFormatter *rfc3339DateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[rfc3339DateFormatter setLocale:enUSPOSIXLocale];
[rfc3339DateFormatter setDateFormat:#"yyyy-MM-dd HH:mm Z"];
[rfc3339DateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *sDate = [rfc3339DateFormatter stringFromDate: _datePicker];
And now is working properly but I'm not sure that this is the correct way.
I hope this helps someone in the future.
UPDATE: This is where I got the code.

Generate NSDate of this type: 2015-04-27T10:45:52Z

I am trying to set the date to PayPal pre-approval key in the following way:
#"2015-04-27T10:45:52Z", #"startingDate",
This date works, however I don't know how to reproduce it in code terms. I tried doing:
NSDateFormatter *dateformate=[[NSDateFormatter alloc]init];
[dateformate setDateFormat:#"yyyy-MM-dd HH:mm:ss zzz"]; // Date formater
NSString *date = [dateformate stringFromDate:[NSDate date]];
but this doesn't work. What is the Z at the end of the date?
First, your date format is not correct. Second, for consistent results, you should always hard-code the en_US_POSIX locale (the date formatter defaults to the user's locale):
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
Alternatively, I've had positive experience with iso-8601-date-formatter. ISO 8601 is a surprisingly complex standard with lots of edge cases, and this library seems to be able to cope with most of them.
The Z stands for Zulu (i.e. UTC/GMT). If you want to generate a date string in that format (GMT with Z qualifier), please refer to Apple's Technical Q&A #1480, which reminds us to specify both locale and timeZone:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
Or in macOS 10.12 and iOS 10, you can do:
NSISO8601DateFormatter *formatter = [[NSISO8601DateFormatter alloc] init];
And then you can do:
NSString *dateString = [formatter stringFromDate:[NSDate date]];

NSString to NSDate conversion using NSDateFormatter > date way off

Trying to convert a datetime from NSString to NSDate using NSDateFormatter. I know it should be a simple task, but it's not - the converted NSDate is whole 20 days off.
I cannot find the cause for this. Already tried every solution I could find (different locales, time zones, formattings ...) but no luck so far.
Here's a code sample:
NSString *strDate = #"24.01.2014 08:17:10";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSLocale *locale = [NSLocale currentLocale];
[dateFormat setDateFormat:#"dd.MM.YYYY HH:mm:ss"];
[dateFormat setLocale:locale];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"CET"]];
NSDate *dte = [dateFormat dateFromString:strDate];
output of dte is "04.01.2014 08:17:10". As you can see, the time is correct, but the date is way off.
There is an error in your time format, YYYY should be yyyy.
NSString *strDate = #"24.01.2014 08:17:10";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSLocale *locale = [NSLocale currentLocale];
[dateFormat setDateFormat:#"dd.MM.yyyy HH:mm:ss"];
[dateFormat setLocale:locale];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"CET"]];
NSDate *dte = [dateFormat dateFromString:strDate];
Will give me as output : 2014-01-24 07:17:10 +0000
Use the following code
NSString *strDate = #"24.01.2014 08:17:10";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd.MM.yyyy HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:strDate];

How to set date format to 24 hour in Objective-c?

I am having problem displaying proper date in my app. When user sets 24 hour format 'ON' in his date time preferences..my app shows correct time but when 24 hour format is set to 'OFF' it shows nothing.
Here is the code I am using:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:style];
[formatter setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
NSDate *temp = [formatter dateFromString:strDate];
[formatter setDateFormat:format];
[formatter setLocale:[NSLocale currentLocale]];
NSString *returnStr = [formatter stringFromDate: temp];
[formatter release];
NSLog(#"Return: %#", returnStr);
return returnStr;
Try this
[dateFormat setDateFormat:#"hh:mm:ss"];
HH : 24 hour format
hh : 12 hour format
So in your case
//after conversion to date
if ON
[formatter setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
if OFF
[formatter setDateFormat:#"YYYY-MM-dd hh:mm:ss"];
Very simple try this:
For 12 Hrs : (hh:mm:ss)
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd hh:mm:ss"];
For 24 Hrs : (HH:mm:ss)
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
use this:
Objective-C :
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
Swift 4.1
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
after that your date won't modified.
Set the locale of the dateFormatter as following. It should sove your problem.
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_GB"];
[dateFormatter setLocale:locale];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd HH:mm"];
NSString *dateString = [dateFormat stringFromDate:[NSDate date]];

Format date and time after device current region settings

I have an NSDate object from which I make two NSStrings: The date and the time. Currently I format the date as 20111031 and time as 23:15.
What I would like to do is to format it to the device (iPhone, iPad, iPod Touch) current region settings (not the language!). So for instance:
A device set to region US would show (from the top of my head) 10.31.11 and time 11:15 pm
A device set to region the Netherlands would show: 31-10-2011 and time 23.15
A device set to region Swedish would show: 2001-10-31 and time 23:15
How can I do this?
The following should be enough, because an NSDateFormatter has the phone's default locale by default:
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
NSLog(#"%#",[dateFormatter stringFromDate:date]);
FYI here's what happens with US, Netherlands, and Sweden:
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
NSLog(#"%#",[dateFormatter stringFromDate:date]);
// displays 10/30/11 7:09 PM
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"nl_NL"]];
NSLog(#"%#",[dateFormatter stringFromDate:date]);
// displays 30-10-11 19:09
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"sv_SE"]];
NSLog(#"%#",[dateFormatter stringFromDate:date]);
// displays 2011-10-30 19:09
Many great code snippets here. One even better in my mind for international date formats (when all I want is the date, not the time) is this as the phone knows the locale language in settings:
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:NSLocaleIdentifier]];
[dateFormatter setTimeStyle:NO];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
NSLog(#"%#",[dateFormatter stringFromDate:date]);

Resources