我正在使用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:SS
用户选择的当前时间date
。
我不知道如何做到这一点,非常感谢任何帮助
更新
目前,当选择日期时,我会
2015-01-15 10:34:35.210 myapp-ios[21476:60b] transactionDate=2015-01-15 08:00:00 +0000
答案 0 :(得分:3)
如果通过"当前时间HH:MM:SS到日期"你的意思是你要用当前的HH:MM:SS 替换所选日期的HH:MM:SS,你可以从所选日期中提取相关的日期成分,得到当前的datetime,提取相关的时间组件,然后将这些组件添加回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];
}
答案 1 :(得分: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];
}