如何结合两个 UIDatePickers 的日期和时间?
Posted
技术标签:
【中文标题】如何结合两个 UIDatePickers 的日期和时间?【英文标题】:How to combine date and time from two UIDatePickers? 【发布时间】:2013-01-13 21:14:18 【问题描述】:我的应用中有两个UIDatePickers
,一种模式用于选择日期,另一种用于选择时间模式。当到达确切的日期和时间时,我已发出通知。我可以在正确的时间触发提醒,但问题是没有检查日期。有什么办法可以同时查看日期和时间吗??
提前谢谢...
编辑
NSDate *date1 = [datePicker date];
NSDate *date2 = [timePicker date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
unsigned unitFlagsDate = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *dateComponents = [gregorian components:unitFlagsDate fromDate:date1];
unsigned unitFlagsTime = NSHourCalendarUnit | NSMinuteCalendarUnit ;
NSDateComponents *timeComponents = [gregorian components:unitFlagsTime fromDate:date2];
[dateComponents setHour:[timeComponents hour]];
[dateComponents setMinute:[timeComponents minute]];
NSDate *combDate = [gregorian dateFromComponents:dateComponents];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil) return;
NSDate *fireTime = combDate;
localNotif.fireDate = fireTime;
localNotif.alertBody = @"Alert!";
localNotif.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
【问题讨论】:
这个google.co.in/…的结果太多了,你有什么问题没有得到解决? @CRDave:我尝试了很多方法。但我反复将合并日期设为 2001-01-01 左右。并且通知在保存的时间到达。 放一些当前代码行,比如你得到日期n时间的格式。作为 NSString 或作为 NSDate 以及你如何组合。我希望它会帮助有人帮助你。 @CRDave 我认为你读错了,它没有比较,它结合了日期和时间.. 您是否已连接您的 IBOutlets,因为我复制了您的代码,并且在 dateComponents 中获得了正确的日期和时间(在您执行 setHour 和 setMinute 之后)。 【参考方案1】:NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComponents = [calendar components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:self.datePicker.date];
NSDateComponents *timeComponents = [calendar components:NSHourCalendarUnit|NSMinuteCalendarUnit fromDate:self.timePicker.date];
NSDateComponents *newComponents = [[NSDateComponents alloc]init];
newComponents.timeZone = [NSTimeZone systemTimeZone];
[newComponents setDay:[dateComponents day]];
[newComponents setMonth:[dateComponents month]];
[newComponents setYear:[dateComponents year]];
[newComponents setHour:[timeComponents hour]];
[newComponents setMinute:[timeComponents minute]];
NSDate *combDate = [calendar dateFromComponents:newComponents];
NSLog(@" \ndate : %@ \ntime : %@\ncomDate : %@",self.datePicker.date,self.timePicker.date,combDate);
【讨论】:
弃用 - 8.0 NSCalendarUnitYear, NSCalendarUnitMonth, NSCalendarUnitDay NSCalendarUnitHour, NSCalendarUnitMinute, NSCalendarUnitSecond【参考方案2】:试试这个:
将日期转换为 NSString,将时间转换为 NSString。
将时间附加到日期字符串中。
现在将最后的字符串转换为 NSDate
示例:(从你方添加验证)
NSDate *date1 = [datePicker date];
NSDate *date2 = [timePicker date];
NSString *date = [NSString stringWithFormat:@"%@",date1];
NSString *time = [NSString stringWithFormat:@"%@",date2];
NSString *dateAndTime = [NSString stringWithFormat:@"%@ %@",date,time];
NSDate *dateTime = [self dateFromString:dateAndTime];
- (NSDate*) dateFromString:(NSString*)aStr
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
//[dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss a"];
[dateFormatter setDateFormat:@"MM/dd/yyyy HH:mm:ss a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSLog(@"%@", aStr);
NSDate *aDate = [dateFormatter dateFromString:aStr];
[dateFormatter release];
return aDate;
【讨论】:
经过一番挣扎,上面的逻辑对我来说很好【参考方案3】:Swift 5
接受的答案的版本,如果它可能有帮助的话。
/// Returns `Date` from date and time.
func combine(date: Date, time: Date) -> Date?
let calendar = Calendar.current
let dateComponents = calendar.dateComponents([.day, .month, .year], from: date)
let timeComponents = calendar.dateComponents([.hour, .minute, .second], from: time)
var newComponents = DateComponents()
newComponents.timeZone = .current
newComponents.day = dateComponents.day
newComponents.month = dateComponents.month
newComponents.year = dateComponents.year
newComponents.hour = timeComponents.hour
newComponents.minute = timeComponents.minute
newComponents.second = timeComponents.second
return calendar.date(from: newComponents)
【讨论】:
以上是关于如何结合两个 UIDatePickers 的日期和时间?的主要内容,如果未能解决你的问题,请参考以下文章
IBM iSeries 结合日期和时间十进制字段来比较当前时间