- (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];
Use this at the point where use defined your datepicker
datePicker.timeZone = [NSTimeZone localTimeZone];
in Swift 3:
datePicker.timeZone = NSTimeZone.local
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With