Reputation: 950
Hello I'm Beginner in ios I Have create Custom UIDatePicker View This show right date and time but when we click on done button then this show date right but time is not right ....So how do this ....
UIDatePicker *DatePicker;
UITexField *DateTime;
if ([[UIScreen mainScreen] bounds].size.height >480)
{
DatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,350,320,50)];
}
else
{
DatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,270,320,50)];
}
DatePicker.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleTopMargin;
DatePicker.datePickerMode = UIDatePickerModeDateAndTime;
DatePicker.date = [NSDate date];
[self.DateTime setInputView:DatePicker];
DatePicker.userInteractionEnabled=YES;
[self.view addSubview:DatePicker];
UIDatePicker *picker = (UIDatePicker*)self.DateTime.inputView;
self.DateTime.text = [NSString stringWithFormat:@"%@",picker.date];
NSLog(@"date picker is %@",self.DateTime.text);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss +HHmm"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSDate *myDate = [dateFormatter dateFromString:[NSString stringWithFormat:@"%@",DateTime.text]];
NSLog(@"%@", [dateFormatter stringFromDate:myDate]);
NSDateFormatter *anotherDateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setAMSymbol:@"AM"];
[dateFormatter setPMSymbol:@"PM"];
[anotherDateFormatter setDateFormat:@"dd/MM/yyyy hh:mm a"];
NSLog(@"%@", [anotherDateFormatter stringFromDate:myDate]);
DateTime.text =[anotherDateFormatter stringFromDate:myDate];
NSLog(@"datetime is %@",DateTime.text);
this picker showh right time and date in UI but when we click done button(not mention here)then it show .... DateTime.text=date picker is 2013-07-03 05:22:57 +0000
after this format it shows 2013-07-03 12:00:57 +0000
03/07/2013 12:00 AM
datetime is 03/07/2013 12:00 AM
When we Select any time then this show only above time ....Date is right ...but time is wrong so ....solve this problem
Upvotes: 0
Views: 1525
Reputation: 187
this code may be help you
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
//[formatter setTimeZone:[NSTimeZone systemTimeZone]];
[formatter setDateFormat:@"dd-MM-yyyy hh:mm"];
strDatetime = [formatter stringFromDate:picker.date];
[datetimeLbl setText:strDatetime];
Upvotes: 1
Reputation:
This may be an issue of NSTimeZone.
You can try this
NSTimeInterval seconds; // assume this exists
NSDate* ts_utc = [NSDate dateWithTimeIntervalSince1970:seconds];
NSDateFormatter* df_utc = [[[NSDateFormatter alloc] init] autorelease];
[df_utc setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
[df_utc setDateFormat:@"yyyy.MM.dd G 'at' HH:mm:ss zzz"];
NSDateFormatter* df_local = [[[NSDateFormatter alloc] init] autorelease];
[df_local setTimeZone:[NSTimeZone timeZoneWithName:@"EST"]];
[df_local setDateFormat:@"yyyy.MM.dd G 'at' HH:mm:ss zzz"];
NSString* ts_utc_string = [df_utc stringFromDate:ts_utc];
NSString* ts_local_string = [df_local stringFromDate:ts_utc];
Upvotes: 0
Reputation: 590
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:@"HH:mm"]; //24hr time format
NSString *dateString = [outputFormatter stringFromDate: picker.date];
NSLog(@"%@",dateString);
It will print the proper time.
Upvotes: 0