16

我的应用程序中有两个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];
4

3 回答 3

23
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);
于 2013-01-30T07:53:22.693 回答
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;
}
于 2013-01-30T06:38:40.483 回答
2

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)
}
于 2020-06-23T09:19:24.537 回答