將字串轉化為時間,犯了一個低級錯誤

來源:互聯網
上載者:User

今天犯了了個很低級的錯誤,想把UIDatePickView的預設選中時間設定為目前時間的下一天的11:30,我的思路:首先擷取目前時間---將目前時間字串化-----截取次字串,分理處當前的年月日中得日----將日+1----然後字串拼接成自己想要的日期格式-------將拼接成的日期轉化成NSDate-------然後設定預設時間

我犯的錯誤,將字串轉化為時間的時候,一直想象NSDate的會有這樣的方法,結果查看NSDate的API沒有查到。犯錯就是沒有第一時間想到:NSDateFormatter.

下面看看我設定的代碼:

 

UIDatePicker * pick = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-215, 320, 215)]; pick.minimumDate = [NSDate date]; NSDate * tempDate = [NSDate dateWithTimeIntervalSinceNow:24*60*60]; NSString * str = [NSString stringWithFormat:@"%@",tempDate]; NSString * result = [[str substringWithRange:NSMakeRange(0, 10)] stringByAppendingFormat:@" 11:30"]; NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy-MM-dd HH:mm"]; NSDate * resultDate = [formatter dateFromString:result]; [pick setDate:resultDate];          [pick addTarget:self action:@selector(pickValueChanged:) forControlEvents:UIControlEventValueChanged]; self.datePicker = pick; NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];//設定為中文顯示 pick.locale = locale; [self.view addSubview:pick]; if (IPhone5) {   self.bgView.frame = CGRectMake(0, -10, 320, self.view.frame.size.height); } else {   self.bgView.frame = CGRectMake(0, -60, 320, self.view.frame.size.height); }  #pragma mark - pick事件 -(void)pickValueChanged:(UIDatePicker *)aPick {     if ([aPick.date timeIntervalSinceDate:[NSDate date]]<0)     {         NSDate * tempDate = [NSDate dateWithTimeIntervalSinceNow:24*60*60];         NSString * str = [NSString stringWithFormat:@"%@",tempDate];         NSString * result = [[str substringWithRange:NSMakeRange(0, 10)] stringByAppendingFormat:@" 11:30"];         NSDateFormatter * formatter = [[NSDateFormatter alloc] init];         [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];         NSDate * resultDate = [formatter dateFromString:result];         [aPick setDate:resultDate];     }     else     {         NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];         [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];         NSString* dateStr = [dateFormatter stringFromDate:aPick.date];         NSString * tempStr = [dateStr substringToIndex:16];         self.text_time.text = tempStr;     } }  UIDatePicker * pick = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-215, 320, 215)];pick.minimumDate = [NSDate date];NSDate * tempDate = [NSDate dateWithTimeIntervalSinceNow:24*60*60];NSString * str = [NSString stringWithFormat:@"%@",tempDate];NSString * result = [[str substringWithRange:NSMakeRange(0, 10)] stringByAppendingFormat:@" 11:30"];NSDateFormatter * formatter = [[NSDateFormatter alloc] init];[formatter setDateFormat:@"yyyy-MM-dd HH:mm"];NSDate * resultDate = [formatter dateFromString:result];[pick setDate:resultDate];       [pick addTarget:self action:@selector(pickValueChanged:) forControlEvents:UIControlEventValueChanged];self.datePicker = pick;NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];//設定為中文顯示pick.locale = locale;[self.view addSubview:pick];if (IPhone5){  self.bgView.frame = CGRectMake(0, -10, 320, self.view.frame.size.height);}else{  self.bgView.frame = CGRectMake(0, -60, 320, self.view.frame.size.height);}#pragma mark - pick事件-(void)pickValueChanged:(UIDatePicker *)aPick{    if ([aPick.date timeIntervalSinceDate:[NSDate date]]<0)    {        NSDate * tempDate = [NSDate dateWithTimeIntervalSinceNow:24*60*60];        NSString * str = [NSString stringWithFormat:@"%@",tempDate];        NSString * result = [[str substringWithRange:NSMakeRange(0, 10)] stringByAppendingFormat:@" 11:30"];        NSDateFormatter * formatter = [[NSDateFormatter alloc] init];        [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];        NSDate * resultDate = [formatter dateFromString:result];        [aPick setDate:resultDate];    }    else    {        NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];        NSString* dateStr = [dateFormatter stringFromDate:aPick.date];        NSString * tempStr = [dateStr substringToIndex:16];        self.text_time.text = tempStr;    }

} 以此總結,告誡自己,不要犯這些蛋疼的問題!

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.