当前位置:编程学习 > wap >>

将字符串转化为时间,犯了一个低级错误

今天犯了了个很低级的错误,想把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;
    }

} 以此总结,告诫自己,不要犯这些蛋疼的问题!

 

补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,