IOS UIDatePicker控件的简单使用

IOS UIDatePicker控件的简单使用

//**************代码*******************
- (void)viewDidLoad {
    [superviewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    NSLog(@"timeZoneName %@",[NSTimeZoneknownTimeZoneNames]);

    NSLog(@"locale....... %@",[NSLocaleavailableLocaleIdentifiers]);

    
    NSLocale *ch_zh_locale = [[NSLocalealloc] initWithLocaleIdentifier:@"zh_Hans_CN"] ;
    //创建datePicker实例变量并初始化
    UIDatePicker *datePicker = [[UIDatePickeralloc] initWithFrame:CGRectMake(0,100, 320 , 216)];//无法改变其高和宽,但是可以获取
    datePicker.locale = ch_zh_locale; //设置中文显示
    [NSDatedate];  //此方法获取的时间是格林尼治时间中国的时间是东八区 + 8小时即可
    NSLog(@"current time    %@",[NSDatedate]);
    NSTimeInterval eigth_Z_CH = 8*60*60;
    NSDate *currentDate = [NSDatedateWithTimeIntervalSinceNow:eigth_Z_CH]; //中国的当前时间
    NSLog(@"......%@",currentDate);
    
    /*
     UIDatePickerModeTime  只显示具体的时间,不显示年月,星期
     UIDatePickerModeDate   只显示 年月日
     UIDatePickerModeDateAndTime  年月日 时间 星期 都显示
     UIDatePickerModeCountDownTimer 显示多少小时 多少分钟
     */
    
    [datePicker setDatePickerMode:UIDatePickerModeDateAndTime];//设置显示模式
    [datePicker setTimeZone:[NSTimeZonetimeZoneWithName:@"Asia/Shanghai"]];//设置时区,上海时区
    [datePicker setMinimumDate:[NSDatedate]]; //设置控件的最小时间,超出最小时间还小则会弹回,此时时间为中国时间
    [datePicker setMaximumDate:[NSDatedateWithTimeIntervalSinceNow:30*24*60*60]];//设置最大时间,超出也会弹回
    [datePicker addTarget:selfaction:@selector(timeChanged:)forControlEvents:UIControlEventValueChanged];//关联方法
    [self.view addSubview:datePicker];
}
- (void)timeChanged:(id)sender
{
    UIDatePicker *currentPicker = (UIDatePicker *) sender;
    NSDate *pickerDate = [currentPicker date];//格林尼治时间
    NSLog(@"datepicker current time %@",[pickerDatedateByAddingTimeInterval:8*60*60]);//  + 8 输出的格式是24制
}
//**********************代码结束*************************

QQ群技术交流:77081333 ,需要代码的可私信


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值