我有一个包含字符串NSString
的{{1}}对象。默认情况下,2013-02-28
显示当前日期,但我想显示UIDatepicker
对象中的任何日期。
我正在将NSString
对象转换为NSString
,我不知道如何在NSDate
UIDatePicker
中设置该日期。我怎么能这样做?我尝试了UIDatePickerModeDate
方法,但它没有用。
答案 0 :(得分:4)
这对你有用。在将NSString发送到NSDatePicker之前,您可能需要将其转换为NSDate。请记住,您可能需要设置NSDateFormater的时区以避免边缘情况。
//Given self.datepicker is your UIDatePicker
NSString *dateString = @"2013-03-28";
NSTimeZone *timezone = [NSTimeZone systemTimeZone];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *date = [dateFormatterGMTAware dateFromString:dateString];
self.datePicker.date = date;
答案 1 :(得分:1)
-(IBAction)btnDateClicked:(id)sender{
theDatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 44.0, self.view.frame.size.width, 0.0)];
theDatePicker.datePickerMode = UIDatePickerModeDate;
theDatePicker.minimumDate=[NSDate date];
// ************To keep the selected date**********
NSString *dateString = [NSString stringWithFormat:@"%@",btnDate.titleLabel.text ];
NSTimeZone *timezone = [NSTimeZone systemTimeZone];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:timezone];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *date = [dateFormatter dateFromString:dateString];
theDatePicker.date = date;
}