I'm sorry to make my first question here a bit of a simple one -- I've spent a day reading the NSLocale and NSCalendar class descriptions but I couldn't see if this was possible.
I have a UIDatePicker in the UIDatePickerModeDateAndTime mode. It is currently displaying date and time according to the user's locale, which is the default behavior.
I would like to be able to offer the option to show the UIDatePicker in either 12-hour or 24-hour time format. Detecting which time format the user is currently using isn't a problem, but I'm not clear on how to change just the time format of UIDatePicker without entirely throwing out the user's locale settings (since the picker also displays the localized days of the week and months). UIDatePicker supports setting its locale and setting its calendar.
So, question one is whether this is something I should be trying to do via NSLocale or NSCalendar, and question two is if anyone can recommend a way to isolate the time format without throwing out the rest of the user's locale settings.
Thanks for any suggestions.
This is not the answer you are looking for, but in Cocoa you could create an NSDateFormatter and attach it to an NSDatePicker (which is an NSControl) using setFormatter. Unfortunately the equivalent iPhone class (UIControl) does not support this yet. I raised a bug with Apple about it and this is a known issue, although they wouldn't tell me if/when they plan to fix/enhance it.
OK, after reading the last two years' worth of others asking the same question here, it seems that there is no way to do this since the UIDatePicker uses the user's country setting instead of the user's locale setting, and that can't be overridden programmatically. I filed a bug.
I have solved this problem by using the user input of AM/PM by initializing another variable as a string of either AM or PM based upon the 12 hour format. The new hour in 24 hour format is put into NSCalendar. To wit:
var hra = 1//ENTER INITIAL HOUR (HH)
var dna = "PM"//ENTER "AM" OR "PM"
if hra <= 12 && dna == "PM"{
hra = hra + 12
}
The variable hra now takes the value of the 24 hour format.
Related
I am trying to write a PFQuery for my application that returns only objects that were created today. With all the depreciations that happened to NSDate and the growing importance of NSDateFormatter I am finding rather hard to go about this.
I have figured the logic to be like this in pseudo-code:
Query q = new Query();
q.whereDateGreaterThan(midnightThisMorning);
q.whereDateLessThan(midnightTonight);
I can't seem to figure out how to get a NSDate object set to 12:00 AM (which would be midnightThisMorning) and another set to 11:59 PM tonight (which would be midnightTonight).
Check out NSCalendar's startOfDayForDate method to find the start of the current day.
To find the end of the current day, you'll want to add a day to the date returned from startOfDayForDate and then subtract one second.
NSDate *createDate = [NSDate dateWithTimeIntervalSince1970:1376460694.103];
NSLog(#"createDate %#",createDate);
I am using the above code to get the current date and time,when I put break point at createDate,It shows correct time stamp value,but NSLog(#"createDate %#",createDate) statement is printing the date as 2013-08-14 06:11:34 +0000.
How to get the correct result?
The date is correct. When printing to the console the description of the date is used and that uses your system locale so it applies your time zone to the date before printing.
When you want to display the time you need to use a date formatter to convert the date into a string. The important part is setting the locale / time zone that the formatter uses.
Take a read of this and this.
I have an app where I set time zones for various cities around the globe. I have no problem doing this and it works great. When the app first loads, it finds your current location (lat & long) and sets the time zone using the device default time zone. I need to return the default time zone in a string, so I can display it. I don't want "GMT" or "EDT", I would like it in the format of "America/New_York" or 'Europe/London". Any ideas?
It sounds like you want this:
NSString *timeZoneName = [[NSTimeZone localTimeZone] name];
That returns "America/New_York" for me, here in the EST time zone.
Or given any NSTimeZone *tz you can get its [tz name], which is the conventional name you are looking for (e.g. "Asia/Tokyo" or "Europe/London".
Look at +[NSTimeZone knownTimeZoneName] for a list of possible names.
I hope that helps.
I don't think there is an object that automatically correlates the time zone to a physical place.
I see you've tagged this objective-c, but in C# you could do something simple like this:
public Enum TimeZone
{
[Description("New York")]
EDT,
[Description("Los Angeles")]
PST
}
public static string GetDescription(Enum value)
{
FieldInfo fi= value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
return (attributes.Length>0)?attributes[0].Description:value.ToString();
}
i'm not sure how easily portable this is to objective-c, and it does require using the reflection library in C#.
Well, you're making the assumption that EDT is the same time zone as America/New_York. It isn't. :) Yes, they're the same UTC offset, but as far as your system is concerned, they are defined independently. There's no strong association between them that Cocoa knows about.
As well, if the user gives you EDT, do you return New_York? Toronto? Panama? There's not a 1:1 correspondence. Oh, and: EDT isn't even unique to a single UTC offset. Australian Eastern Daylight Time is also abbreviated EDT, I'm told by the Cocoa docs.
There is a [NSTimeZone abbreviationDictionary] map between abbreviations and long names, but again, they're arbitrarily chosen when there's more than one association (such as New York and Panama.)
What is it you're trying to accomplish in a broader sense? What's your goal? Tell us and we may be able to suggest an alternate way to achieve it. :)
I'm developing an app for iPhone that uploads pictures to a webserver. These pictures have the time of when they were taken in the filename. Since they can be taken from anywhere in the World, I have to keep attention to timezones and DST. I thought I can use CFAbsoluteTimeGetCurrent, that shouldn't be localized ([NSDate date] returns the localized version of time, with or without "AM", "PM", "p.m." or any other variant... for example it returns Arabian characters, if your phone is set in arabic language!).
So, can you suggest me a function to convert CFAbsoluteTimeGetCurrent to a "MySQL like" date, something like "2011-11-03 14:12:10"?
My second question is: what about timezones and daylight saving? CFAbsoluteTimeGetCurrent always returns an UTC date, no matter how the iPhone timezone is set? Is it always DST-free?
Of course I know that the local iPhone date/time could be wrong, but milliseconds-precision is not important for my application :)
Thank you in advance!
What on earth do you mean by
([NSDate date] returns the localized version of time, with or without "AM", "PM", "p.m." or any other variant... for example it returns Arabian characters, if your phone is set in arabic language!).
[NSDate date] returns an NSDate* object. I'm assuming what you really mean is the output of -[NSDate description] returns a string localized in the user's current locale, but then the question is, why are you depending on the output of -[NSDate date]? If you need to format a date a certain way, you should use an NSDateFormatter.
CFAbsoluteTime represents a moment in time, independent of time zones or localization settings. If you want to format a date (i.e. an NSDate object) you should look at NSDateFormatter, which lets you specify the exact format and localization (if any) of the output string. (If you need to work at the Core Foundation level, CFDateFormatter does the same thing.)
I'm working on setting up NSDateFormatter to explain the date, and I'd like something short but more intuitive than 15/07/10. I think I've seen some formats that will say simply "Today" or "Tomorrow" or the day of the week for subsequent days of the same week. Is there a simple apple-approved way to get this type of date?
Thanks.
If you're targeting iOS 4 or later, you can call [yourFormatter setDoesRelativeDateFormatting:YES]. Otherwise, you'll most likely need a custom subclass. As for days of the week, what jer said.
Ensure you set up your locale with the NSDateFormatter appropriately, and then ask for the weekdaySymbols. This will return an array with the days of the week in the locale you specify.