NSDateComponents of an NSDate

后端 未结 4 678
广开言路
广开言路 2020-12-17 08:07

How do I get the NSDateComponents of a single NSDate? I don\'t want the components of the difference between 2 dates, just the seconds, minutes, hours, day, month and year o

相关标签:
4条回答
  • 2020-12-17 08:47

    There's an example in the Apple docs, Listing 3: Getting a date’s components:

    NSDate *today = [NSDate date];
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *weekdayComponents =
                        [gregorian components:(NSDayCalendarUnit | 
                                               NSWeekdayCalendarUnit) fromDate:today];
    NSInteger day = [weekdayComponents day];
    NSInteger weekday = [weekdayComponents weekday];
    

    Note that you have to 'or' together the calendar units you want in the call of the components method.

    0 讨论(0)
  • 2020-12-17 08:50

    in Swift 2.x the components you want are passed like this.

    let gregorianCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian) 
    let weekdayHourMinutes = gregorianCalendar?.components([.Weekday, .Hour, .Minute], fromDate: date)
    
    0 讨论(0)
  • 2020-12-17 09:03

    According to Apple Developer documentation, DateComponents has second, minute, hour, day, month, year instance properties. The following Swift 5 Playground code shows how to get seconds, minutes, hours, day, month and year components from a Date instance using DateComponents:

    import Foundation
    
    var calendar = Calendar(identifier: .gregorian)
    calendar.timeZone = TimeZone(abbreviation: "CET")!
    let date = Date()
    
    // Get seconds, minutes, hours, day, month and year
    let components = calendar.dateComponents([.second, .minute, .hour, .day, .month, .year], from: date)
    let seconds = components.second
    let minutes = components.minute
    let hours = components.hour
    let day = components.day
    let month = components.month
    let year = components.year
    
    // Print components
    print(String(describing: seconds)) // prints Optional(33)
    print(String(describing: minutes)) // prints Optional(42)
    print(String(describing: hours)) // prints Optional(22)
    print(String(describing: day)) // prints Optional(17)
    print(String(describing: month)) // prints Optional(12)
    print(String(describing: year)) // prints Optional(2016)
    
    /* ... */
    
    // Set a formatter in order to display date
    let formatter = DateFormatter()
    formatter.dateStyle = .short
    formatter.timeStyle = .long
    formatter.timeZone = TimeZone(abbreviation: "CET")
    formatter.locale = Locale(identifier: "fr_FR")
    print(formatter.string(from: date)) // prints 17/12/2016 22:42:33 UTC+1
    
    0 讨论(0)
  • 2020-12-17 09:04

    using NSCalendar's method:

    - (NSDateComponents *)components:(NSUInteger)unitFlags fromDate:(NSDate *)date

    like:

    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
    NSDate *date = [NSDate date];
    
    
    NSCalendar * cal = [NSCalendar currentCalendar];
    NSDateComponents *comps = [cal components:unitFlags fromDate:date];
    
    0 讨论(0)
提交回复
热议问题