如何将当前时间(HH:MM:SS)添加到 NSDate?

How to add current Time(HH:MM:SS) to NSDate?

我正在使用 https://github.com/ruslanskorb/RSDayFlow 将日历添加到我的应用程序。

我将方法重写为

// Prints out the selected date.
- (void)datePickerView:(RSDFDatePickerView *)view didSelectDate:(NSDate *)date {

    self.transactionDate = date;
    self.inputDateField.text =  [[Helper getDateFormatterForClient] stringFromDate:self.transactionDate];
    NSLog(@"transactionDate=%@", self.transactionDate);
    [self.inputDateField resignFirstResponder];

}

我想添加的只是 HH:MM:SSdate 用户选择的当前时间。

我不知道该怎么做,非常感谢任何帮助

更新

Currently, when the date is selected, I get

2015-01-15 10:34:35.210 myapp-ios[21476:60b] transactionDate=2015-01-15 08:00:00 +0000 

如果 "current time HH:MM:SS to the date" 的意思是要 替换 所选日期的 HH:MM:SS 为当前的 HH:MM:SS,您可以提取所选日期的相关日期组件,获取当前日期时间,提取相关时间组件,然后将这些组件添加回 self.transactionDate,例如:

// Prints out the selected date.
- (void)datePickerView:(RSDFDatePickerView *)view didSelectDate:(NSDate *)date {

    self.transactionDate = date;
    self.inputDateField.text =  [[Helper getDateFormatterForClient] stringFromDate:self.transactionDate];
    NSLog(@"transactionDate=%@", self.transactionDate);
    [self.inputDateField resignFirstResponder];

    // Create an instance of the current calendar
    NSCalendar *calendar = [NSCalendar currentCalendar];

    // Break self.transactionDate into day, month, and year components
    NSDateComponents* dateComponents = [[NSCalendar currentCalendar] components:NSCalendarUnitYear|
        NSCalendarUnitMonth|NSCalendarUnitDay fromDate:self.transactionDate];

    // Save transaction date with just the day, month, and year
    self.transactionDate = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];

    // Get the current datetime
    NSDate *currentDate = [NSDate date];

    // Extract the hour, minute, and second components
    NSDateComponents *components = [calendar components:(NSCalendarUnitHour|
        NSCalendarUnitMinute|NSCalendarUnitSecond) fromDate:currentDate];

    // Add those hour, minute, and second components to
    // self.transactionDate
    self.transactionDate = [calendar dateByAddingComponents:components 
        toDate:self.transactionDate options:0];

}

我在“NSDate”类别中创建的这个方法。

+ (NSDate *)getDateWithCurrentTime:(NSDate *)date {

    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];


    NSDateComponents *components =
    [gregorian components:(NSCalendarUnitDay |
                           NSCalendarUnitYear|NSCalendarUnitMonth) fromDate:date];


    NSDateComponents *CurrentDateComponent =
    [gregorian components:(NSCalendarUnitHour |
                           NSCalendarUnitMinute|NSCalendarUnitSecond) fromDate:[NSDate date]];

    [components setHour:[CurrentDateComponent hour]];
    [components setMinute:[CurrentDateComponent minute]];
    [components setSecond:[CurrentDateComponent second]];
    [components setTimeZone:[NSTimeZone systemTimeZone]];

    return [gregorian dateFromComponents:components];

}