NSDate包含来自另一个NSDate的日期组件和来自另一个NSDate的时间组件

时间:2012-06-24 21:29:33

标签: ios nsdate uidatepicker

我的应用程序中有两个UIDatePicker控件,一个配置了UIDatePickerModeDate,另一个配置了UIDatePickerModeTime。

我想配置一个NSDate对象,其中包含来自UIDatePicker的日期和来自第二个的时间......如何?

谢谢, 标记

4 个答案:

答案 0 :(得分:57)

假设date1和date2是您的两个NSDate值。 date1包含您的日期组件,date2包含您的时间组件。以下是将它们组合在一起的方法:

NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

// Extract date components into components1
NSDateComponents *components1 = [gregorianCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
                                                     fromDate:date1];

// Extract time components into components2
NSDateComponents *components2 = [gregorianCalendar components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit 
                                                     fromDate:date2];

// Combine date and time into components3
NSDateComponents *components3 = [[NSDateComponents alloc] init];

[components3 setYear:components1.year];
[components3 setMonth:components1.month];
[components3 setDay:components1.day];    

[components3 setHour:components2.hour];
[components3 setMinute:components2.minute];
[components3 setSecond:components2.second];         

// Generate a new NSDate from components3.
NSDate *combinedDate = [gregorianCalendar dateFromComponents:components3];   

// combinedDate contains both your date and time!

适用于iOS 8.0 +

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

// Extract date components into components1
NSDateComponents *components1 = [gregorianCalendar components:(NSCalendarUnit)(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay)
                                                     fromDate:date1];

// Extract time components into components2
NSDateComponents *components2 = [gregorianCalendar components:(NSCalendarUnit)(NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond)
                                                     fromDate:date2];

// Combine date and time into components3
NSDateComponents *components3 = [[NSDateComponents alloc] init];

[components3 setYear:components1.year];
[components3 setMonth:components1.month];
[components3 setDay:components1.day];

[components3 setHour:components2.hour];
[components3 setMinute:components2.minute];
[components3 setSecond:components2.second];

// Generate a new NSDate from components3.
NSDate * combinedDate = [gregorianCalendar dateFromComponents:components3];

// combinedDate contains both your date and time!

答案 1 :(得分:8)

在斯威夫特:

let calendar = NSCalendar.currentCalendar()
let date1 = NSDate() // PickerDateMode
let date2 = NSDate() // PickerHourMode

let componentsOne = calendar.components([.Year, .Month, .Day], fromDate: date1)
let componentsTwo = calendar.components([.Hour, .Minute], fromDate: date2)   

let totalComponents = NSDateComponents()
totalComponents.year = componentsOne.year
totalComponents.month = componentsOne.month
totalComponents.day = componentsOne.day
totalComponents.hour = componentsTwo.hour
totalComponents.minute = componentsTwo.minute

let finalDate = calendar.dateFromComponents(totalComponents)

答案 2 :(得分:5)

NSCalendar *calendar= [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDate *date = [NSDate date];//datapicker date
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:date];

NSInteger year = [dateComponents year];
NSInteger month = [dateComponents month];
NSInteger day = [dateComponents day];
NSInteger hour = [dateComponents hour];
NSInteger minute = [dateComponents minute];
NSInteger second = [dateComponents second];

答案 3 :(得分:1)

更新到Swift 4和4.1

 func combineDateAndTimeToOneDate() {

    let datefrmter = DateFormatter()
     datefrmter.dateFormat = "dd/MM/yyyy"
    let dateFromString = datefrmter.date(from: lblDate.text!)

    datefrmter.dateFormat = "h:mm a"
    let timeFromString = datefrmter.date(from: lblTime.text!)

    let calendar = NSCalendar.current

    let componentsOne = calendar.dateComponents([.year,.month,.day], from: dateFromString!)
    let componentsTwo = calendar.dateComponents([.hour,.minute,.second,.nanosecond], from: timeFromString!)

    let totalComponents = NSDateComponents()
    totalComponents.year = componentsOne.year!
    totalComponents.month = componentsOne.month!
    totalComponents.day = componentsOne.day!
    totalComponents.hour = componentsTwo.hour!
    totalComponents.minute = componentsTwo.minute!
    totalComponents.second = componentsTwo.second!
    totalComponents.nanosecond = componentsTwo.nanosecond!

    let finalDate = calendar.date(from: totalComponents as DateComponents)
    print(finalDate)

}

希望它有所帮助!