【问题标题】:How to add current Time(HH:MM:SS) to NSDate?如何将当前时间(HH:MM:SS)添加到 NSDate?
【发布时间】:2015-01-15 18:44:25
【问题描述】:

我正在使用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 

【问题讨论】:

标签: ios iphone ios7 nsdate


【解决方案1】:

如果“当前时间 HH:MM:SS 到日期”是指您想要替换所选日期的 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。
  • 谢谢@Lyndsey,我对同时使用 NSDate、NSDateComponents 和 NSCalendar 感到困惑。
【解决方案2】:

我在 `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];

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    • 2012-11-21
    • 2015-06-10
    相关资源
    最近更新 更多