Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify a time zone in UIDatePicker

- (IBAction) buttonPressed {
NSDate *selected = [datePicker date];
NSString *message = [[NSString alloc] initWithFormat:@"The date and time you selected is: %@", selected];
UIAlertView *alert = [[UIAlertView alloc]
                      initWithTitle:@"Date and Time Selected" message:message delegate:nil cancelButtonTitle:@"Yes, I did!" otherButtonTitles:nil];
[alert show];
[alert release];
[message release];

So my question is next... How can I specify a time zone using a Datepicker in Objective C? now if I choose some values from DatePicker and press the button which displays an alert - the time is wrong. It's probably because of incorrect time zone but I'm not sure. Well any ideas would be great. On the picture below you can see that I chose 2:14 PM (14:14) but an alter says that it is 11:14 and the time zone is +000

UPDATE 1 I have added a few changes to my code but still nothing...

- (IBAction) buttonPressed {
NSDate *selected = [datePicker date];       
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];    
NSTimeZone *tz = [NSTimeZone systemTimeZone];
[dateFormatter setTimeZone:tz];
NSString *formattedDate = [dateFormatter stringFromDate:selected];
NSString *message = [[NSString alloc] initWithFormat:@"The date and time you selected is: %@", formattedDate];
UIAlertView *alert = [[UIAlertView alloc]
                      initWithTitle:@"Date and Time Selected" 
                      message:message delegate:nil cancelButtonTitle:@"Yes, I did!" otherButtonTitles:nil];
[alert show];
[alert release];
[message release];
[dateFormatter release];
like image 303
Sapozhnick Avatar asked Jun 18 '11 11:06

Sapozhnick


2 Answers

Use this at the point where use defined your datepicker

datePicker.timeZone = [NSTimeZone localTimeZone];

like image 92
Avik Roy Avatar answered Oct 17 '22 00:10

Avik Roy


in Swift 3:

datePicker.timeZone = NSTimeZone.local
like image 23
Wilson Avatar answered Oct 17 '22 01:10

Wilson