Jak uzyskać NSDateComponents jednego NSDate? Nie chcę składników różnicy między 2 datami, tylko sekundy, minuty, godziny, dzień, miesiąc i rok NSDate?NSDateComponents of NSDate
21
A
Odpowiedz
38
Jest przykładem w Apple docs Listing 3: Pierwsze elementy randkę za:
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];
Należy pamiętać, że trzeba „lub” razem calendar units chcesz w wywołaniu metody components
.
6
stosując metodę NSCalendar za:
- (NSDateComponents *)components:(NSUInteger)unitFlags fromDate:(NSDate *)date
jak:
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDate *date = [NSDate date];
NSCalendar * cal = [NSCalendar currentCalendar];
NSDateComponents *comps = [cal components:unitFlags fromDate:date];
0
w Swift 2.x składniki, które mają są przekazywane w taki sposób.
let gregorianCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
let weekdayHourMinutes = gregorianCalendar?.components([.Weekday, .Hour, .Minute], fromDate: date)
2
Według Apple Developer API Reference, DateComponents
ma second
, właściwości minute
, hour
, day
, month
, year
instancji.
Poniższy kod SWIFT 3 zabaw pokazuje jak zdobyć sekundy, minuty, godziny, dzień, miesiąc i rok komponenty z instancji Date
użyciu 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(reflecting: seconds)) // prints Optional(33)
print(String(reflecting: minutes)) // prints Optional(42)
print(String(reflecting: hours)) // prints Optional(22)
print(String(reflecting: day)) // prints Optional(17)
print(String(reflecting: month)) // prints Optional(12)
print(String(reflecting: 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
Powiązane problemy
- 1. NSDate z NSDateComponents
- 2. Różnice w składni NSDateComponents?
- 3. Jak uzyskać wszystkie niedziele w Array of date ios
- 4. Inicjalizuj obiekt NSDate o określonym czasie.
- 5. W NSDateComponents, skąd wiadomo, czy to AM czy PM?
- 6. Tworzenie przyszłą datę w Swift z NSDate()
- 7. Jak otrzymać NSDate na 00:00 w dniu [data NSDate]?
- 8. Swift: Drukowanie wszystkich dat pomiędzy dwoma NSDate()
- 9. NSDate, porównując dwie daty
- 10. Co oznacza era w NSDateComponents?
- 11. Porównywanie NSDate do [data NSDate]
- 12. Jak wygenerować dogodne zakresy dat na podstawie danego NSDate?
- 13. NSDate zwraca niewłaściwą datę pierwszego dnia miesiąca
- 14. NSDate z NSString
- 15. jeśli NSDate to poniedziałek
- 16. NSDate - rok temu, dylemat
- 17. NSDate - Ustawianie przyszłej daty
- 18. NSDate ze stringa
- 19. iOS - Przyjazny format NSDate
- 20. NSDate dzień roku (swift)
- 21. Jak NSLog NSDate?
- 22. Nazwa dnia z NSDate?
- 23. Dodawanie dni do NSDate
- 24. Http Data do NSDate
- 25. ObservableCollection (Of T) vs BindingList (Of T)?
- 26. W Objective-C, aby uzyskać bieżącą godzinę i minutę jako liczby całkowite, musimy użyć NSDateComponents i NSCalendar?
- 27. NSDate: Dodaj dzień na podstawie czasu
- 28. Jak zmienić NSString w NSDate?
- 29. iPhone SDK NSString do NSDate
- 30. Jak odjąć godziny od NSDate?
mnie pokonać, ha! +1 –
@martin clayton, Cześć, Ustawiłem konkretny czas, godziny i sekundę do NSDateComponents, a teraz chcę ponownie utworzyć datę z tymi NSDateComponents. Jak mogę to zrobić? –