In my ipad app the users can enter the date. Is there a way to accept and display the date format for any country the app is being use in?
How are they entering the date? Are you using UIDatePicker?
NSDateFormatter[1] will respect the current Locale on the phone.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSLog([dateFormatter stringFromDate:[NSDate date]]);
[1] http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html%23//apple_ref/occ/instm/NSDateFormatter/dateStyle
Related
I am testing different time zones UTC offsets in application. And finally this code is properly working almost with all timezones. But i have an issue with Caracas.
Code that shows UTC offset.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setLocale:enUSPOSIXLocale];
//This NSDateFormatter will return timezone in format "UTC+XX:XX"
[dateFormatter setDateFormat:#"'UTC'xxxxx"];
NSString *formattedTimeZone = [dateFormatter stringFromDate:[NSDate date]];
return formattedTimeZone;
In Ukraine i receive UTC+03:00 and it is correct. In Caracas i receive UTC-04:00 but real offset is UTC-04:30.
Question is why i am missing -30 minutes in Caracas?
This is not a programming problem, Caracas(Venezuela) timezone has changed recently.
UTC-04:00 is correct right now.
Presidents of Venezuela had changed this a couple of times:
UTC-04:30 was used since 2007.
It was recently changed again to UTC-04:00.
http://www.bloomberg.com/news/articles/2016-04-14/maduro-orders-time-zone-change-to-battle-venezuela-power-crisis
I want to hide the past as well as future dates from my UIDatePicker. Only want to show today's date. Is it possible?
Right now all the dates are coming, but only today's date is selecting. Time is also there. So in short I want to select today's date time by showing today's date only.
Than no need to add DatePicker.
NSDateFormatter *formatter;
NSString *dateString;
formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd-MM-yyyy"];
dateString = [formatter stringFromDate:[NSDate date]];
I've recently been working on a French version of our application use NSLocalizedStrings, so far everything is working great.
But my problem now is with dates. I show dates often in my application, in different formats, depending on the situation.
Ex:
-Fri Feb 22, 2013
-Monday February 18, 2013
-Feb 18
-Dec 5, 2012
The thing is, dates in French are not only different in terms of the name of the month, but also the order in which month, day and year appear.
Ex:
-Dec 5, 2012 would be 5 Dec 2012
-Monday February 18, 2013 would be Lundi le 18 Fevrier 2013.
I have the individual month/day names in my Localizable.string files, but how do I manage the order of how it's displayed.
Should I have an if statement that checks the current device language like so?:
NSString *currentLanguage = [[NSLocale preferredLanguages] objectAtIndex:0];
if([currentLanguage isEqualToString:#"fr"])
{
//Handle French logic
}
This is probably not the best way to go about it.
Any ideas?
You should use an NSDateFormatter and feed it your desired NSLocale like this:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"nl_NL"];
dateFormatter.dateFormat = #"EEEE d MMMM yyyy HH:mm";
'EEEE' is the full name of the day of the week, which, in my case, will be displayed in dutch.
Use NSDateFormatter. For example:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:162000];
NSString *formattedDateString = [dateFormatter stringFromDate:date];
NSLog(#"formattedDateString: %#", formattedDateString);
NSDateFormatterMediumStyle will automatically format the date according to the user's preference (English, French, etc.).
If you need custom styling and the app runs in iOS 4.0+, you can use a custom template in your date formatter:
NSString *formatString = [NSDateFormatter dateFormatFromTemplate:#"EdMMMyyy" options:0
locale:[NSLocale currentLocale]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:formatString];
NSString *todayString = [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"todayString: %#", todayString);
Set proper locale to your NSDateFormatter when you convert date to string, then date formatter will handle all specifics of formats depending on user settings for you:
NSDateFormatter *formatter = ... // Create and setup formatter
[formatter setLocale:[NSLocale autoupdatingCurrentLocale]];
// Now you can convert date to string
That's probaly much easier:
There is something which is called
NSDateFormatterShortStyle,
NSDateFormatterMediumStyle
NSDateFormatterLongStyle
Set Date and Time components individually:
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
Ios will format that correctly if the language of the user.
Further look at Data Formatting Guide
I would try the class function localizedStringFromDate:dateStyle:timeStyle: in NSDateFormatter before attempting to use the NSDateFormatter and a template (from Unicode Technical Standard #35):
Example:
[NSDateFormatter localizedStringFromDate:dateTime dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterNoStyle];
You can select both different date and time outputs of various lengths detailed in the docs.
Apple documentation: NSDateFormatter localizedStringFromDate:dateStyle:timeStyle:
My iOS app is going to display the current time. How do I determine whether the user's locale prefers 12-hour or 24-hour time formatting?
See the NSDateFormatter class http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html
Something like:
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterNoStyle];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSString *dateString = [dateFormatter stringFromDate:date];
You generally don't need to figure out what the user's locale settings are when displaying data to the user. If you use the relevant NSFormatter (NSNumberFormatter for numbers and NSDateFormatter for date and time, which is the relevant one for you in this case), then you will automatically get the output that is appropriate to the user's locale... i.e what Apple calls the "localized representation of the object"
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]);