将日期和时间合并到单个 NSDate 中时出现问题

发布于 2024-11-06 09:21:15 字数 1055 浏览 2 评论 0原文

我正在尝试使用此方法将一个 NSDate 的时间值(小时、分钟、秒)与第二个 NSDate 的日期值(日、月、年)结合起来:

+ (NSDate *)combineDate:(NSDate *)date withTime:(NSDate *)time {

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

    NSDateComponents *dateComponents = [gregorian components:(NSDayCalendarUnit | 
        NSWeekdayCalendarUnit) fromDate:date];
    NSDateComponents *timeComponents = [gregorian components:(NSDayCalendarUnit | 
        NSWeekdayCalendarUnit) fromDate:time];

    NSDateComponents *comp = [NSDateComponents alloc];
    [comp setSecond:[timeComponents second]];
    [comp setHour:[timeComponents hour]];
    [comp setMinute:[timeComponents minute]];
    [comp setYear:[dateComponents year]];
    [comp setMonth:[dateComponents month]];
    [comp setDay:[dateComponents day]];

    NSDate *combDate = [gregorian dateFromComponents:comp]; 

    [gregorian release];
    [comp release];

    return combDate;
}

...但是它返回的实际上是乱码(例如,年份显示为“1”)。有人看出这个方法有什么问题吗?

I am trying to combine the time value (hours, minutes, seconds) of one NSDate with the date value (day, month, year) of a second NSDate with this method:

+ (NSDate *)combineDate:(NSDate *)date withTime:(NSDate *)time {

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

    NSDateComponents *dateComponents = [gregorian components:(NSDayCalendarUnit | 
        NSWeekdayCalendarUnit) fromDate:date];
    NSDateComponents *timeComponents = [gregorian components:(NSDayCalendarUnit | 
        NSWeekdayCalendarUnit) fromDate:time];

    NSDateComponents *comp = [NSDateComponents alloc];
    [comp setSecond:[timeComponents second]];
    [comp setHour:[timeComponents hour]];
    [comp setMinute:[timeComponents minute]];
    [comp setYear:[dateComponents year]];
    [comp setMonth:[dateComponents month]];
    [comp setDay:[dateComponents day]];

    NSDate *combDate = [gregorian dateFromComponents:comp]; 

    [gregorian release];
    [comp release];

    return combDate;
}

... but what it's returning is effectively gibberish (the year comes out as "1", for example). Anybody see what's wrong with this method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

和我恋爱吧 2024-11-13 09:21:16

这是 Swift 2 中的

func combineDateAndTime(date: NSDate, time: NSDate) -> NSDate {

    let calendar = NSCalendar.currentCalendar()
    let dateComponents = calendar.components([.Year, .Month, .Day], fromDate: date)
    let timeComponents = calendar.components([.Hour, .Minute, .Second], fromDate: time)

    let components = NSDateComponents()
    components.year = dateComponents.year
    components.month = dateComponents.month
    components.day = dateComponents.day
    components.hour = timeComponents.hour
    components.minute = timeComponents.minute
    components.second = timeComponents.second

    return calendar.dateFromComponents(components)!
}

Here it is in Swift 2

func combineDateAndTime(date: NSDate, time: NSDate) -> NSDate {

    let calendar = NSCalendar.currentCalendar()
    let dateComponents = calendar.components([.Year, .Month, .Day], fromDate: date)
    let timeComponents = calendar.components([.Hour, .Minute, .Second], fromDate: time)

    let components = NSDateComponents()
    components.year = dateComponents.year
    components.month = dateComponents.month
    components.day = dateComponents.day
    components.hour = timeComponents.hour
    components.minute = timeComponents.minute
    components.second = timeComponents.second

    return calendar.dateFromComponents(components)!
}
不美如何 2024-11-13 09:21:16

(Swift + 扩展) 是你的朋友:

// MARK: - NSDate extension -

extension NSDate {

    static func combineDate(date date: NSDate, withTime time: NSDate) -> NSDate? {
        let gregorian = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)

        let dateComponents = gregorian?.components([.Year, .Month, .Day], fromDate: date)
        let timeComponents = gregorian?.components([.Hour, .Minute, .Second], fromDate: date)

        dateComponents?.second = timeComponents?.second ?? 0
        dateComponents?.minute = timeComponents?.minute ?? 0
        dateComponents?.hour = timeComponents?.hour ?? 0

        if let _ = dateComponents {
            return gregorian?.dateFromComponents(dateComponents!)
        }
        return nil
    }

}

(Swift + extension) are your friend:

// MARK: - NSDate extension -

extension NSDate {

    static func combineDate(date date: NSDate, withTime time: NSDate) -> NSDate? {
        let gregorian = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)

        let dateComponents = gregorian?.components([.Year, .Month, .Day], fromDate: date)
        let timeComponents = gregorian?.components([.Hour, .Minute, .Second], fromDate: date)

        dateComponents?.second = timeComponents?.second ?? 0
        dateComponents?.minute = timeComponents?.minute ?? 0
        dateComponents?.hour = timeComponents?.hour ?? 0

        if let _ = dateComponents {
            return gregorian?.dateFromComponents(dateComponents!)
        }
        return nil
    }

}
清欢 2024-11-13 09:21:15

我使用了错误的日历组件。这是更正后的函数(现在适用于 ARC):

+ (NSDate *)combineDate:(NSDate *)date withTime:(NSDate *)time {

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

    unsigned unitFlagsDate = NSYearCalendarUnit | NSMonthCalendarUnit 
        |  NSDayCalendarUnit;
    NSDateComponents *dateComponents = [gregorian components:unitFlagsDate 
        fromDate:date];
    unsigned unitFlagsTime = NSHourCalendarUnit | NSMinuteCalendarUnit 
        |  NSSecondCalendarUnit;
    NSDateComponents *timeComponents = [gregorian components:unitFlagsTime 
        fromDate:time];

    [dateComponents setSecond:[timeComponents second]];
    [dateComponents setHour:[timeComponents hour]];
    [dateComponents setMinute:[timeComponents minute]];

    NSDate *combDate = [gregorian dateFromComponents:dateComponents];   

    return combDate;
}

I was using the wrong calendar components. Here's the corrected function (now for ARC):

+ (NSDate *)combineDate:(NSDate *)date withTime:(NSDate *)time {

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

    unsigned unitFlagsDate = NSYearCalendarUnit | NSMonthCalendarUnit 
        |  NSDayCalendarUnit;
    NSDateComponents *dateComponents = [gregorian components:unitFlagsDate 
        fromDate:date];
    unsigned unitFlagsTime = NSHourCalendarUnit | NSMinuteCalendarUnit 
        |  NSSecondCalendarUnit;
    NSDateComponents *timeComponents = [gregorian components:unitFlagsTime 
        fromDate:time];

    [dateComponents setSecond:[timeComponents second]];
    [dateComponents setHour:[timeComponents hour]];
    [dateComponents setMinute:[timeComponents minute]];

    NSDate *combDate = [gregorian dateFromComponents:dateComponents];   

    return combDate;
}
白龙吟 2024-11-13 09:21:15

斯威夫特 3 扩展:

func combineDateAndTime(date: Date, time: Date) -> Date {

    let calendar = NSCalendar.current

    let dateComponents = calendar.dateComponents([.year, .month, .day], from: date)
    let timeComponents = calendar.dateComponents([.hour, .minute, .second], from: time)

    var components = DateComponents()
    components.year = dateComponents.year
    components.month = dateComponents.month
    components.day = dateComponents.day
    components.hour = timeComponents.hour
    components.minute = timeComponents.minute
    components.second = timeComponents.second

    return calendar.date(from: components)!
}

Swift 3 extension:

func combineDateAndTime(date: Date, time: Date) -> Date {

    let calendar = NSCalendar.current

    let dateComponents = calendar.dateComponents([.year, .month, .day], from: date)
    let timeComponents = calendar.dateComponents([.hour, .minute, .second], from: time)

    var components = DateComponents()
    components.year = dateComponents.year
    components.month = dateComponents.month
    components.day = dateComponents.day
    components.hour = timeComponents.hour
    components.minute = timeComponents.minute
    components.second = timeComponents.second

    return calendar.date(from: components)!
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文