點(diǎn)擊按鈕 要求:
(1)早上8.00到下午18.00 之內(nèi)可選擇時(shí)間 之外不允許
(2)小于當(dāng)前時(shí)間的不能選
-(void)Time
{
//獲取系統(tǒng)時(shí)間
NSDate * senddate=[NSDate date];
NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"HH"];
int locationString =[[dateformatter stringFromDate:senddate] intValue];
//判斷時(shí)間(8 - 18)
if (locationString >= 8 && locationString <= 18)
{
NSLog(@"在時(shí)間范圍內(nèi)");
UIAlertController* alertVc=[UIAlertController alertControllerWithTitle:@"\n\n\n\n\n\n\n\n\n\n\n" message:nil preferredStyle:(UIAlertControllerStyleActionSheet)];
UIDatePicker* datePicker=[[UIDatePicker alloc]init];
datePicker.datePickerMode = UIDatePickerModeDateAndTime;
UIAlertAction* ok=[UIAlertAction actionWithTitle:@"確認(rèn)" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {
// nsdate 類型的時(shí)間
NSDate* chosedate=[datePicker date];
NSDateFormatter* formatter=[[NSDateFormatter alloc]init];
//選中最小時(shí)間是現(xiàn)在
NSDate * nowDate=[NSDate date];
datePicker.minimumDate = nowDate;
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
// 字符串類型的時(shí)間
NSString* curentDatest=[formatter stringFromDate:chosedate];
//輸出選中時(shí)間是多少
NSString *text=curentDatest;
//選中時(shí)間的時(shí)間戳是多少
NSLog(@"%@",text);
// 選中時(shí)間的時(shí)間戳
// NSString *timeSp=[NSString stringWithFormat:@"%ld",(long)[chosedate timeIntervalSince1970]];
if ([nowDate timeIntervalSince1970] - [chosedate timeIntervalSince1970] <0)
{
NSLog(@"時(shí)間能選");
}else
{
NSLog(@"時(shí)間bu能選");
}
}];
UIAlertAction* no=[UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:nil];
[alertVc.view addSubview:datePicker];
[alertVc addAction:ok];
[alertVc addAction:no];
[self presentViewController:alertVc animated:YES completion:nil];
}else
{
NSLog(@"不在時(shí)間范圍內(nèi)");
}
}```