Yang saya coba lakukan adalah mendapatkan NSDate hari ini, kemarin, Minggu ini, Minggu lalu, Bulan ini, variabel Bulan lalu siap untuk perbandingan untuk tajuk yang akan ditambahkan pada UITableView's titleForHeaderInSection
Yang saya inginkan dilakukan secara manual pada kode di bawah ini untuk tanggal 2009-12-11
NSDate *today = [NSDate dateWithString:@"2009-12-11 00:00:00 +0000"];
NSDate *yesterday = [NSDate dateWithString:@"2009-12-10 00:00:00 +0000"];
NSDate *thisWeek = [NSDate dateWithString:@"2009-12-06 00:00:00 +0000"];
NSDate *lastWeek = [NSDate dateWithString:@"2009-11-30 00:00:00 +0000"];
NSDate *thisMonth = [NSDate dateWithString:@"2009-12-01 00:00:00 +0000"];
NSDate *lastMonth = [NSDate dateWithString:@"2009-11-01 00:00:00 +0000"];
iphone
time
comparison
nsdate
hasnat
sumber
sumber
NSCalendar
jika ketelitian diperlukan.Mungkin cara yang lebih baik untuk menulis ini tetapi inilah yang saya temukan atas saran NSCalendar Ben dan bekerja dari sana ke NSDateComponents
NSCalendar *cal = [NSCalendar currentCalendar]; NSDateComponents *components = [cal components:( NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond ) fromDate:[[NSDate alloc] init]]; [components setHour:-[components hour]]; [components setMinute:-[components minute]]; [components setSecond:-[components second]]; NSDate *today = [cal dateByAddingComponents:components toDate:[[NSDate alloc] init] options:0]; //This variable should now be pointing at a date object that is the start of today (midnight); [components setHour:-24]; [components setMinute:0]; [components setSecond:0]; NSDate *yesterday = [cal dateByAddingComponents:components toDate: today options:0]; components = [cal components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:[[NSDate alloc] init]]; [components setDay:([components day] - ([components weekday] - 1))]; NSDate *thisWeek = [cal dateFromComponents:components]; [components setDay:([components day] - 7)]; NSDate *lastWeek = [cal dateFromComponents:components]; [components setDay:([components day] - ([components day] -1))]; NSDate *thisMonth = [cal dateFromComponents:components]; [components setMonth:([components month] - 1)]; NSDate *lastMonth = [cal dateFromComponents:components]; NSLog(@"today=%@",today); NSLog(@"yesterday=%@",yesterday); NSLog(@"thisWeek=%@",thisWeek); NSLog(@"lastWeek=%@",lastWeek); NSLog(@"thisMonth=%@",thisMonth); NSLog(@"lastMonth=%@",lastMonth);
sumber
fromDate:[[NSDate alloc] init]
potongan - potongan itu.NSDateComponents bagus untuk didapatkan hari ini:
NSCalendar *cal = [NSCalendar currentCalendar]; NSDate *date = [NSDate date]; NSDateComponents *comps = [cal components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:date]; NSDate *today = [cal dateFromComponents:comps];
Ini membuat NSDate hanya dengan tahun, bulan dan tanggal:
(gdb) po today 2010-06-22 00:00:00 +0200
Untuk mendapatkan kemarin, dll. Anda dapat menghitungnya menggunakan NSDateComponents:
NSDateComponents *components = [[NSDateComponents alloc] init]; [components setDay:-1]; NSDate *yesterday = [cal dateByAddingComponents:components toDate:today options:0];
sumber
(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
seharusnya(NSEraCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
.+ (NSDate*)dateFor:(enum DateType)dateType { NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *comps = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:[NSDate date]]; if(dateType == DateYesterday) { comps.day--; } else if(dateType == DateThisWeek) { comps.weekday = 1; } else if(dateType == DateLastWeek) { comps.weekday = 1; comps.week--; } else if(dateType == DateThisMonth) { comps.day = 1; } else if(dateType == DateLastMonth) { comps.day = 1; comps.month--; } else if(dateType != DateToday) return nil; return [calendar dateFromComponents:comps]; }
sumber
DateThisWeek
, menyetel hari kerja tidak berdampak pada NSDate ...Swift 4.2.0
let today = Date() let yesterday = today.addingTimeInterval(-86400.0) let thisWeek = today.addingTimeInterval(-604800.0) let lastWeek = today.addingTimeInterval(-1209600.0) let thisMonth = today.addingTimeInterval(-2629743.83) let lastMonth = today.addingTimeInterval(-5259487.66) // components of the date let calendar = Calendar(identifier: Calendar.Identifier.gregorian) let components = calendar.dateComponents([.year, .month, .day], from: today) let (year, month, day) = (components.year, components.month, components.day)
Harap dicatat bahwa komponen tanggal adalah opsional.
sumber
Jawaban lain tidak berhasil untuk saya (mungkin karena zona waktu saya). Inilah cara saya melakukannya:
- (BOOL)isOnThisWeek:(NSDate *)dateToCompare { NSCalendar * calendar = [NSCalendar currentCalendar]; NSDate * today = [NSDate date]; int todaysWeek = [[calendar components: NSWeekCalendarUnit fromDate:today] week]; int dateToCompareWeek = [[calendar components: NSWeekCalendarUnit fromDate:dateToCompare] week]; int todaysYear = [[calendar components:NSYearCalendarUnit fromDate:today] year]; int dateToCompareYear = [[calendar components:NSYearCalendarUnit fromDate:dateToCompare] year]; if (todaysWeek == dateToCompareWeek && todaysYear == dateToCompareYear) { return YES; } return NO; }
sumber
Jika Anda menggunakan iOS 10+ atau MacOS 10.12+, Anda dapat menggunakan dua
Calendar
metode ini untuk melakukannya dengan benar.func date(byAdding component: Calendar.Component, value: Int, to date: Date, wrappingComponents: Bool = default) -> Date?
( dokumen )func dateInterval(of component: Calendar.Component, for date: Date) -> DateInterval?
( dokumen )Berikut adalah contoh cara menggunakan metode ini di Swift 3, bersama dengan output taman bermain di zona waktu saya.
let calendar = Calendar.current let now = Date() // => "Apr 28, 2017, 3:33 PM" let yesterday = calendar.date(byAdding: .day, value: -1, to: now) // => "Apr 29, 2017, 3:33 PM" let yesterdayStartOfDay = calendar.startOfDay(for: yesterday!) // => ""Apr 29, 2017, 12:00 AM" let thisWeekInterval = calendar.dateInterval(of: .weekOfYear, for: now) // => 2017-04-23 04:00:00 +0000 to 2017-04-30 04:00:00 +0000 let thisMonthInterval = calendar.dateInterval(of: .month, for: now) // => 2017-04-01 04:00:00 +0000 to 2017-05-01 04:00:00 +0000 let aDateInLastWeek = calendar.date(byAdding: .weekOfYear, value: -1, to: now) let lastWeekInterval = calendar.dateInterval(of: .weekOfYear, for: aDateInLastWeek!) // => 2017-04-16 04:00:00 +0000 to 2017-04-23 04:00:00 +0000 let aDateInLastMonth = calendar.date(byAdding: .month, value: -1, to: now) let lastMonthInterval = calendar.dateInterval(of: .weekOfYear, for: aDateInLastMonth!) // => 2017-03-26 04:00:00 +0000 to 2017-04-02 04:00:00 +0000
Bonus: Anda dapat menggunakan
DateInterval
s untuk menguji apakah tanggal termasuk dalam kisaran itu. Melanjutkan dari atas:thisWeekInterval!.contains(now) // => true lastMonthInterval!.contains(now) // => false
sumber
yesterday
dikatakan 29 Apr dannow
28 Apr?NSDate *today = [NSDate date]; // Today's date NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *weekdayComponents =[gregorian componentsNSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:today]; NSInteger day = [weekdayComponents day];
sumber
Saya sangat menyukai objek THCalendarInfo yang terdapat dalam proyek ini:
http://github.com/jaredholdcroft/kcalendar
Saya tidak dapat menemukan yang asli. Dengan menggunakan objek ini, Anda dapat pindah ke hari sebelumnya, awal minggu, awal bulan, mendapatkan hari dalam seminggu, hari dalam sebulan ... dll.
sumber
NSDate *today = [[NSDate alloc] initWithTimeIntervalSinceNow:0];
sumber
Ini untuk mengecek tanggalnya bulan ini atau tidak
func isOnThisMonth(dateToCompare: NSDate) -> Bool { let calendar: NSCalendar = NSCalendar.currentCalendar() let today: NSDate = NSDate() let todaysWeek: Int = calendar.components(NSCalendarUnit.Month, fromDate: today).month let dateToCompareWeek: Int = calendar.components(.Month, fromDate: dateToCompare).month let todaysYear: Int = calendar.components(NSCalendarUnit.Year, fromDate: today).year let dateToCompareYear: Int = calendar.components(NSCalendarUnit.Year, fromDate: dateToCompare).year if todaysWeek == dateToCompareWeek && todaysYear == dateToCompareYear { return true } return false }
Dan yang kedua hanya mengubah jenis calendarUnit yang lemah seperti ini
func isOnThisWeek(dateToCompare: NSDate) -> Bool { let calendar: NSCalendar = NSCalendar.currentCalendar() let today: NSDate = NSDate() let todaysWeek: Int = calendar.components(NSCalendarUnit.Weekday, fromDate: today).weekday let dateToCompareWeek: Int = calendar.components(.Weekday, fromDate: dateToCompare).weekday let todaysYear: Int = calendar.components(NSCalendarUnit.Year, fromDate: today).year let dateToCompareYear: Int = calendar.components(NSCalendarUnit.Year, fromDate: dateToCompare).year if todaysWeek == dateToCompareWeek && todaysYear == dateToCompareYear { return true } return false }
Saya harap ini membantu seseorang. Terima kasih.
sumber
Aku menjawab pertanyaan serupa sudah dan inilah mengapa jawaban saya adalah lebih baik:
sumber