Format date and time after device current region settings - ios

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]);

Related

Getting wrong time while converting from NSString to NSDate When Device Time format is 24hr

In Following Code I am trying to convert NSString to NSDate and once again converting to another Date format and sending it to server.
Following code worked perfectly fine when my Device Time Format is 12 hr. with dateformat to [dateFormatter setDateFormat:#"dd-MMM-yyyy hh:mm a"]; it gives me correct Date and Time.
But As soon as I changed device time Format to 24 hr. and change the datefomatter to [dateFormatter setDateFormat:#"dd-MMM-yyyy HH:mm a"]; it gives wrong Date and Time (Specifically time.) I had shown log values for code.
I had checked lot of stackoverflow questions regarding NSDateFormatter and Conversion of NSString to NSDate. But I am not able to find out where I'm making mistake and why it gives me wrong time when my Device time format is 24hr. Please help me to resolve this.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MMM-yyyy HH:mm a"];
NSString *fromTime = [NSString stringWithFormat:#"%#",#"04-Jul-2017 01:46 PM"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:fromTime];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSString *setdtFrom=[dateFormatter stringFromDate:dateFromString];
NSLog(#"From DateTime %#",setdtFrom);
Following log showing Date and Time When My Device Time format is 12hr. And it is Correct one.
From DateTime 2017-07-04T13:46:00
Time When My Device Time format is 24hr. And it is wrong (specifically time).
From DateTime 2017-07-04T12:46:00
Please try setting locale & timezone
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MMM-yyyy hh:mm a"];
[dateFormatter setLocale: [[NSLocale alloc] initWithLocaleIdentifier: #"en_US_POSIX"]];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSString *fromTime = [NSString stringWithFormat:#"%#",#"04-Jul-2017 01:46 PM"];
NSDate *dateFromString = [dateFormatter dateFromString:fromTime];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSString *setdtFrom=[dateFormatter stringFromDate:dateFromString];
NSLog(#"From DateTime %#",setdtFrom);

Converting calendar date to the correct date

I am working on an application that creates alerts with calendar. I can correctly set alarms on correct dates. For example, I set an alarm for 4th of May 2017 1 PM.
When, I try to get the calendar event it returns me some other date in UTC.
As you can see, it returns me 10 AM on same day with UTC. I am wondering how can I get the exact date when I try to get it from calendar.
You just need to convert UTC to your local timezone.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSDate *date1 = [dateFormatter dateFromString:#"2017-05-04 10:00:00"];
// change to a readable time format and change to local time zone
[dateFormatter setDateFormat:#"MM/dd/yyyy hh:mm a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *strCurrentLocalTimezoneDate = [dateFormatter stringFromDate:date1];
Date always takes current time zone until we changed other.If we print the Date it might be showing different but actually it takes current.
// except this code you may have to set timeZone as well.
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"MMM-dd-yyyy"];
NSDate *now = [[NSDate alloc] init];
NSString *dateString = [format stringFromDate:now];
NSLog(#"%#",dateString);

How to display date format in application according to region selected in iPhone setting(Language & Region)

Below is the code I am using to show the date according to region selected. But is always shows the January 25, 2016. Not changing according to region change. E.g For U.S.: January 5, 2016 and U.K: 5 January 2016.
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:NSLocaleIdentifier]];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
NSLog(#"%#",[dateFormatter stringFromDate:date]);
If you want the date formatted based on the user's locale, don't set the locale. It will already default to the user's locale.
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
NSLog(#"%#",[dateFormatter stringFromDate:date]);
BTW - your use of NSLocaleIdentifier isn't correct. That's some internal constant, not a value you would ever pass to initWithLocaleIdentifier:.

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.

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

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];

Resources