I got a UITimePicker in my app, and I want to use it to select a certain number of hours and minutes after a user event. (for example 3 hours 15 minutes after 7PM) This is why I want to show the date in 24 hour format, otherwise the user might get confused.
Is there a way to force the timepicker to show 24 hour view, independent of the clock format for the locale? Or do I just pick another locale?
UIDatePicker *timePicker;
NSDateFormatter *timePickerFormatter;
[self.view addSubview:timePicker];
float tempHeight = timePicker.frame.size.height;
timePicker.frame = CGRectMake(0,150 ,self.view.frame.size.width , tempHeight);
timePicker.hidden = YES;
timePickerFormatter= [[NSDateFormatter alloc]init ];
[timePickerFormatter setDateFormat:#"HH:mm"]; //formats date for processing
Thank you!
Update: I found the answer like this:
timepicker.datePickerMode = UIDatePickerModeCountDownTimer;
This effectively accomplishes what I'm trying to do by displaying 0 to 23 hours and 0 to 59 minutes, no further customization required! Sorry for asking about the 24 hour format, this is what the Android app that I'm porting over was using!
Did you try to use the calendar and locale properties of UIDatePicker?
That should be the solution for your problem, because as the documentation explains (read the UIDatePicker class reference):
UIDatePickerModeTime
The date picker displays hours, minutes, and (optionally) an AM/PM designation. The exact items shown and their order depend upon the locale set. An example of this mode is [ 6 | 53 | PM ].
[EDIT] after further investigation, it seems the NSLocale can't override the AM/PM setting… (I had that information by… simply searching here in Stackoverflow! And your question has already been asked and answered many times before)
For visualization I use:
[self.datePickerView setLocale:[NSLocale localeWithLocaleIdentifier:#"ru_RU"]];
You may use your own locale.
And for NSDateFormatter I Use:
[_dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
It is no longer breaks my applications.
Related
Hi I am creating an NSDate which I am using to set a Picker, but when I output the picker date to NSLog it is always an hour behind the time shown in the actual picker.
Also as a test I noticed that the code:
NSDate *now = [NSDate date]; //should give me the current date and time on my emulator
NSLog(#"Date and Time now: %#", now);
The NSLog value is always 1 hour behind the time shown on my emulator.
My question is why?
Thanks in advance :)
UPDATE --------------------------------------------------------------------------------
I did a couple of more tests and it turns out that NSDate using the NSGregorian Calendar was returning the correct time and then NSDate using the currentCalendar was off by 1 hour.
So my new question is why is that? I have checked the settings on my emulator and the device has its calendar set to Gregorian. Could this be due to a timezone difference? If so then is there a way for me to set the picker to use the same timezone as the users device?
Thanks
I think it is because your simulator time is set to default time zone (EN). NSDate is always set from your local time. When you try it on your device it will be correct.
i have this code in my project and work fine:
NSDate*now = [NSDate date];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"dd.MM.yyyy"];
i create date and store it to my CoreData. Always that time will my actual time on my device.
Congrats to your new iPhone anyway. Welcome to big Apple Family! :)
NSDate *currentDate = [NSDate date];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"MM/dd/yyyy,h,mm,a"];
NSString *dateString = [format stringFromDate:currentDate];
[format release];
NSLog(dateString);
Output in: Device iPhone4
[240:707] 10/25/2011,22,23,
Current language: auto; currently objective-c
warning: Unable to read symbols for /SDK4.2/Platforms/iPhoneOS.platform/DeviceSupport/4.3.3 (8J2)/Symbols/Developer/usr/lib/libXcodeDebuggerSupport.dylib
Output in: Device iPad2
[82:707] 10/25/2011,12,28,PM
Current language: auto; currently objective-c
warning: Unable to read symbols for /SDK4.2/Platforms/iPhoneOS.platform/DeviceSupport/4.3.3 (8J2)/Symbols/Developer/usr/lib/libXcodeDebuggerSupport.dylib
This thing works fine on Simulator for all.
How to handle this?
More Info:
it is also to notice that the same formatting string resulting in 12 hr and 24 hr clock values in return
Sounds like you have a different locale set on your iPad and your iPhone, see here:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW1
Either set the same locale on both devices (if it's just your problem) or make sure the NSDateFormatter uses the same (hard-coded) locale if you rely on the format being like a certain format.
Edit: Additional Info
It apparently IS a problem with the locale, more detailed here (with solutions):
http://developer.apple.com/library/ios/#qa/qa1480/_index.html
But I was wrong, setting the locale will not help it indeed.
Citation:
On iPhone OS, the user can override the default AM/PM versus 24-hour time setting (via Settings > General > Date & Time > 24-Hour Time), which causes NSDateFormatter to rewrite the format string you set, which can cause your time parsing to fail.
finally resolved it.
it was due to the settings in phone as 24 Hr clock.
I had to update my code accordingly.
:)
Hey all so as the title suggest I just have a date picker and it appears to be 4 hours and 56 minutes fast.. which is very strange. Code is very straight foreword:
NSLog(#"%#",datePicker.date);
In the view did load
datePicker.timeZone = [NSTimeZone localTimeZone];
Any ideas/suggestions?
ETA: for example if I set the time as 4 00 PM I get this in the NSLog
0001-01-01 20:56:02 +0000
Set minimumDate and maximumDate on your date picker to something sane.
Dates before October 1582 tend to have numerous issues in iOS, due to some things recognizing the Julian/Gregorian calendar transition and other things not. There also seems to be accuracy issues in the times when you deal NSDates near the year 1.
I am creating a calendar based app.
I have two dates say present date, and future date(due date), now i need to show the difference between these two dates in terms of days, hours, minutes & seconds and i want to show a timer which will continues to decrement second by second as the time increases and ultimately reaches to zero when due date comes.Basically it will be showing that how much days, hrs and seconds are left for the event.
How can I do that, please help me.
It would be a great help if i can get a similar kind of sample code.
Many Thanks in advance.
iPhone Developer
Maybe It can be helpful to you - http://blog.webscale.co.in/?p=244
Basically you want to use NSTimeInterval and NSDate to calculate the difference in time. For example:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy"];
NSDate *now = [NSDate date];
NSDate *futureDate = [dateFormat dateFromString:#"07/15/2015"];
// Now we can calculate the time interval, which really is an integer
// This is the number of seconds between the 2 dates, so with math you can calculate
// what you need. Remember, when you do dateFromString, the time is midnight 00:00:00
// however, *now is set to the current time. You may want to work on that a bit
NSTimeInterval timeDifference = [futureDate timeIntervalSinceDate:now];
// Also note, if you swap the order and do now timeIntervalSinceDate:futureDate, you
// get a negative number.
I have an array of NSDates which I build from strings using [NSDate dateFromString]
In the xml I parsed to get the string there was also a timezone string. As far as I can see in the manual NSDate does not in it self deal with timezones. Do I need to always store this timezone value somewhere and pair it with the belonging NSDate each time I need it?
I also need to figure out that if an event starts in London at 10:00, but I am in Denmark having my iPhone set to danish time my "event started in London" should display at 09:00 o'clock.
Again if an event starts in London at 10:00 o'clock and ends in Denmark at 12:00 o'clock, If I were to compare start time and end time using an iPhone with danish settings I would get that the duration of the event was 02:00 event though 10:00 o'clock in UK and 12:00 o'clock in Denmark is only 1 hour apart.
NSdate works really well for these things in the scope of one timezone, but introducing the timezone part just made everything complicated to me. Is there a way to abstract/hide all these calculations, as I see potential for making a lot of mistakes.
I have been through the NSDateformatter and NSDate guides from Apple, but they are really vague and sports a substantial amount of deprecated code :/
Thanks for any help given.
You should take one standard timezone like UTC/GMT format for all calculation.
According to the NSDate reference, dateWithString: takes an offset to GMT as last component; while it is not a time zone, it is sufficient to perform computation or comparison).
Looking at the NSTimeZone reference, you can use the abbreviationForDate: and the timeZoneWithAbbreviation: to get a NSTimeZone object from a NSDate instance. Once you get the time zone, you have everything you need.
I convert the present date and the date I would like to know if is close, to GMT and then returning the difference. So I changed every thing to deal with differences instead of actual times and dates. A bit like a music score transposed to a different key:)
+ (NSInteger) minutesUntilDate:(NSDate*) date withTimezoneOffset:(NSInteger) GMTOffset
{
NSDate *now = [NSDate date];
NSTimeInterval localTimeZoneOffset = [[NSTimeZone defaultTimeZone] secondsFromGMT];
now = [now addTimeInterval:(localTimeZoneOffset * -1)];
date = [date addTimeInterval:(GMTOffset * 60 * 60) * −1];
return ((NSInteger)[now timeIntervalSinceDate:date] / 60 ) * -1;
}
As soon as you have allocated an NSDate, these do not have timezone information any longer. NSDate is "timezone-less" and is always in GMT. You should make sure that NSDate understand your format correctly when allocating it.
Once you have an NSDate you can make normal calculations and ignore the timezones.
You only need to take care of timezones when reading strings into NSDates and when printing them out.