This is a little more complicated then just
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"startDate >= %# AND endDate <= %#", startDay,endDay];
I'm building a calendar app and I need to pull events out of my core data store that occur on a given day. However, it's possible for an event to start/end on a different day then the day I'm trying to display, e.g.
My Date Range (Start = 2011-12-02 00:00:00 to End = 2011-12-02 23:59:59)
An Event (Start = 2011-12-01 23:00:00 to End = 2011-12-02 05:00:00)
How can I write a predicate to determine if that event falls in that date range. Keep in mind that an event could start before and after the date range.
Thanks!
Here is a method to build a predicate to retrieve non recurring events occurring on a given day (recurring events require additional processing):
- (NSPredicate *) predicateToRetrieveEventsForDate:(NSDate *)aDate {
// start by retrieving day, weekday, month and year components for the given day
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *todayComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:aDate];
NSInteger theDay = [todayComponents day];
NSInteger theMonth = [todayComponents month];
NSInteger theYear = [todayComponents year];
// now build a NSDate object for the input date using these components
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:theDay];
[components setMonth:theMonth];
[components setYear:theYear];
NSDate *thisDate = [gregorian dateFromComponents:components];
[components release];
// build a NSDate object for aDate next day
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setDay:1];
NSDate *nextDate = [gregorian dateByAddingComponents:offsetComponents toDate:thisDate options:0];
[offsetComponents release];
[gregorian release];
// build the predicate
NSPredicate *predicate = [NSPredicate predicateWithFormat: #"startDate < %# && endDate > %#", nextDate, thisDate];
return predicate;
}
The predicate is almost equal to the one proposed by #Tony, except it does not check for equality. Here is why. Suppose you have an event starting on December 8 23:00 and ending at December 9 00:00. If you check for events whose ending date is >= rather than > of the given day, your app will report the event in both December 8 and 9, which is clearly wrong. Try adding such an event to both iCal and Google Calendar, and you will see that the event only appears on December 8. In practice, you should not assume that a given day ends at 23:59:59 (even though this is of course true): you need to treat midnight as the last second of a given day (with regard to the end of an event). Also, note that this does not prevent events starting at midnight.
What you want is:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"startDate <= %# AND endDate >= %#", endDay, startDay];
In other words, you're eliminating events that start after the end of the range or end before the start, i.e., events that have an empty intersection with the range.
Massimo Camaro had a great answer. I took the liberty of porting it to Swift and modifying it a little to support a different column name.
Swift 2.0
func predicateToRetrieveEventsForDate(aDate:NSDate, predicateColumn:String) -> NSPredicate {
// start by retrieving day, weekday, month and year components for the given day
let gregorian = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
let todayComponents = gregorian?.components([.Day, .Month, .Year], fromDate: aDate)
let theDay = todayComponents?.day
let theMonth = todayComponents?.month
let theYear = todayComponents?.year
// now build a NSDate object for the input date using these components
let components = NSDateComponents()
components.day = theDay!
components.month = theMonth!
components.year = theYear!
let thisDate = gregorian?.dateFromComponents(components)
// build a NSDate object for aDate next day
let offsetComponents = NSDateComponents()
offsetComponents.day = 1
let nextDate = gregorian?.dateByAddingComponents(offsetComponents, toDate: thisDate!, options: NSCalendarOptions(rawValue: 0))
// build the predicate
let predicate = NSPredicate(format: "\(predicateColumn) >= %# && \(predicateColumn) < %#", thisDate!, nextDate!)
return predicate
}
While the proposed solution is correct, it is also quite verbose. I have re-implemented it in Swift 3.0:
import Foundation
extension NSPredicate {
static func filter(key: String, onDayRangeForDate date: Date, calendar: Calendar = Calendar(identifier: .gregorian)) -> NSPredicate {
let dateComponents = calendar.dateComponents([.day, .month, .year], from: date)
let startOfDay = calendar.date(from: dateComponents)!
let offsetComponents = NSDateComponents()
offsetComponents.day = 1
let endOfDay = calendar.date(byAdding: offsetComponents as DateComponents, to: startOfDay)!
return NSPredicate(format: "\(key) >= %# && \(key) < %#",
startOfDay as NSDate, endOfDay as NSDate)
}
}
Related
I am setting up an NSPredicate for a fetch to Core Data which is set up like so:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(date >= %#) AND (date <= %#)", todaysDate, tomorrowsDate];
I need todaysDate to be today at 00:00:00, and I need tomorrowsDate to be today at 23:59:59.
I can't set todaysDate equal to [NSDate date] and then manipulate the hours, minutes, and seconds with NSDateComponents because [NSDate date] gives me a date which is 5 hours ahead of my local actual time (so if it's 11:00 pm here on May 6th, then [NSDate date] would give me "2014-05-07 04:00:00 +0000", but I still need it to think that it is May 6th, not 7th!).
How can I manipulate the tools I have in Xcode to consistently get my variable todaysDate to be today at midnight, and tomorrowsDate to be a second before midnight strikes tomorrow?
The rangeOfUnit:... method of NSCalendar is a convenient method to
compute the start of the current day and the start of tomorrow
in your local time zone:
NSDate *now = [NSDate date];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *todaysDate;
NSDate *tomorrowsDate;
NSTimeInterval interval;
[cal rangeOfUnit:NSDayCalendarUnit startDate:&todaysDate interval:&interval forDate:now];
tomorrowsDate = [todaysDate dateByAddingTimeInterval:interval];
so that you can use it in the predicate with >= and <:
[NSPredicate predicateWithFormat:#"(date >= %#) AND (date < %#)", todaysDate, tomorrowsDate]
to fetch all objects of the current day.
Remark: Don't let the NSLog() output of NSDate objects confuse you.
NSDate represents an absolute point in time and knows nothing about time zones.
NSLog(#"%#", todaysDate) prints the date according to the GMT time zone and not in your local time zone.
To print the dates according to your time zone, use p todaysDate in the debugger console (instead of po),
or print
[todaysDate descriptionWithLocale:[NSLocale currentLocale]]
The following works for me:
- (NSDate *)dateWithDate:(NSDate *)date Hour:(NSInteger)hour Minute:(NSInteger)minute Second:(NSInteger)second {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date];
components.hour = hour;
components.minute = minute;
components.second = second;
return [calendar dateFromComponents:components];
}
Example:
NSDate *beginningOfDay = [self dateWithDate:[NSDate date] Hour:0 Minute:0 Second:0];
NSDate *endOfDay = [self dateWithDate:[NSDate date] Hour:23 Minute:59 Second:59];
Swift:
let cal = NSCalendar.currentCalendar()
//tip:NSCalendarUnit can be omitted, but with the presence of it, you can take advantage of Xcode's auto-completion
var comps = cal.components(NSCalendarUnit.YearCalendarUnit | .MonthCalendarUnit | .DayCalendarUnit | .HourCalendarUnit | .MinuteCalendarUnit | .SecondCalendarUnit, fromDate: NSDate())
comps.hour = 0
comps.minute = 0
comps.second = 0
let todaysDate = cal.dateFromComponents(comps)!
let tomorrowsDate = NSDate(timeInterval: 86399, sinceDate: todaysDate)
Objective-C:
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *comps = [cal components: (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate: [NSDate date]];
[comps setHour:0];
[comps setMinute:0];
[comps setSecond:0];
NSDate *todaysDate = [cal dateFromComponents:comps];
NSDate *tomorrowsDate = [NSDate dateWithTimeInterval: 86399 sinceDate:todaysDate];
I have a UILocalNotification object that I have setup with repeat intervals day, week, and month. I am having no troubles at all accessing the fire date of the object:
[cell.detailTextLabel setText:[notification1.fireDate description]];
But I am having troubles getting the next fire date. If I print out the above notification1 object to the console, I get this:
<UIConcreteLocalNotification: 0x613e060>{fire date = 2010-11-29 03:53:52 GMT, time zone = America/Denver (MST) offset -25200, repeat interval = 16, next fire date = 2010-11-30 03:53:52 GMT}
This object contains somewhere the value or data I need to display the next fire date...but I can't find it! Does anybody know where I can get it programmatically?
Thanks
To calculate the next fire date for a repeating UILocalNotification, you have to:
Figure out the amount of repeatInterval there's been between the notification's original fire date (i.e. its fireDate property) and now.
Add them to the notification's fireDate.
Here's one approach:
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *difference = [calendar components:notif.repeatInterval
fromDate:notif.fireDate
toDate:[NSDate date]
options:0];
NSDate *nextFireDate = [calendar dateByAddingComponents:difference
toDate:notif.fireDate
options:0];
This works in many scenarios, but here's a scenario where it won't work:
Suppose that:
the notification's `fireDate is 01/01 at 2:00pm
the notification's repeatInterval is NSDayCalendaryUnit (i.e. repeat daily)
The date now is 08/01 at 3:00pm
The above code will calculate the difference to 7 days (01/01 + 7 days = 08/01), add them to fireDate, and thus set nextFireDate to 08/01 at 2pm. But that's in the past, we want nextFireDate to be 09/01 at 2pm!
So if using the above code and your repeatInterval is NSDayCalendaryUnit, then add these lines:
if ([nextFireDate timeIntervalSinceDate:[NSDate date]] < 0) {
//next fire date should be tomorrow!
NSDateComponents *extraDay = [[NSDateComponents alloc] init];
extraDay.day = 1;
nextFireDate = [calendar dateByAddingComponents:extraDay toDate:nextFireDate options:0];
}
I marked this answer as community wiki, feel free to edit it if you have found a better way to do the calculation!
I don't think the next fire date is available as a property but rather calculated from fireDate and repeatInterval. Date calculating can be tricky with different time zones and other nasty things. In your example you have chosen a daily repeat and to calculate the next fire date you can do something along the lines of:
NSCalendar *calendar = localNotif.repeatCalendar;
if (!calendar) {
calendar = [NSCalendar currentCalendar];
}
NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
components.day = 1;
NSDate *nextFireDate = [calendar dateByAddingComponents:components toDate:localnotif.fireDate options:0];
If you use some other repeat interval you would have to change the code accordingly. If you were to use NSMonthCalendarUnit you would have to use components.month = 1 instead.
i would just add the repeatInterval until the date lies in the future:
-(NSDate*)nextFireDateForNotification:(UILocalNotification*)notification {
NSCalendar *calendar = notification.repeatCalendar;
if (!calendar) {
calendar = [NSCalendar currentCalendar];
}
NSDate* date = [notification.fireDate copy];
while (date.timeIntervalSinceNow > 0) {
date = [calendar dateByAddingUnit:notification.repeatInterval value:1 toDate:date options:0];
}
return date;
}
This is in Swift 4 and using calendar's nextDate func.
extension UILocalNotification {
var nextFireDate: Date? {
guard let fireDate = fireDate else { return nil }
let today = Date()
let cal = Calendar.current
if fireDate.compare(today) == .orderedDescending {
return fireDate
}
let s: Set<Calendar.Component>
switch repeatInterval {
case .year: s = [.month, .day, .hour, .minute, .second]
case .month: s = [.day, .hour, .minute, .second]
case .day: s = [.hour, .minute, .second]
case .hour: s = [.minute, .second]
case .minute: s = [.second]
default: return nil // Not supporting other intervals
}
let components = cal.dateComponents(s, from: fireDate)
return cal.nextDate(after: today, matching: components, matchingPolicy: .nextTimePreservingSmallerComponents)
}
}
Let's say we are the 15/07/2010, I would like to get the first day of the previous week, which is the 5 of July. I have the following method but it does not work. Seems that I have a problem with the weekday attribute...
+ (NSDate *)getFirstDayOfPreviousWeek
{
// Get current date
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now = [NSDate date];
// Get today date at midnight
NSDateComponents *components = [cal components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSTimeZoneCalendarUnit) fromDate:now];
[components setHour:0];
[components setMinute:0];
[components setSecond:0];
NSInteger *dayNbr = [components weekday];
NSLog(#"week day:%#",dayNbr); // CRASH
NSDate *todayMidnight = [cal dateFromComponents:components];
// Get first day of previous week midnight
components = [[[NSDateComponents alloc] init] autorelease];
NSInteger *wd = [dayNbr intValue] * -1 + 1;
[components setWeekDay:wd];
[components setWeek:-1];
NSDate *newDate = [cal dateByAddingComponents:components toDate:todayMidnight options:0];
[cal release];
NSLog(#"new date:%#", newDate);
return newDate;
}
Could you please help ?
Thanks a lot,
Luc
I think it should be a lot easier
- (NSDate *)getFirstDayOfPreviousWeek
{
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now = [NSDate date];
NSLog(#"Current date: %#", now);
// required components for today
NSDateComponents *components = [cal components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit ) fromDate:now];
// adjust them for first day of previous week (Monday)
[components setWeekday:2];
[components setWeek:([components week] - 1)];
// construct new date and return
NSDate *newDate = [cal dateFromComponents:components];
NSLog(#"New date: %#", newDate);
return newDate;
}
Note this solution assumes the first day of the week is Monday.
The following Swift 3 Playground code shows how to get the first day of the previous week from any date:
import Foundation
let calendar = Calendar(identifier: .gregorian)
let intialDate = Date()
// Prepare the new date components
var newDateComponents = calendar.dateComponents([.year, .month, .weekOfMonth], from: intialDate)
newDateComponents.weekOfMonth = newDateComponents.weekOfMonth! - 1 // get previous week
newDateComponents.weekday = 2 // sunday is 1, monday is 2
// Get the new date
let newDate = calendar.date(from: newDateComponents)
print(newDate ?? "Could not retrieve a date")
If you need to repeat this operation, you may refactor your code into a function or a method:
import Foundation
func firstDayOfPreviousWeek(from date: Date) -> Date? {
let calendar = Calendar(identifier: .gregorian)
var newDateComponents = calendar.dateComponents([.year, .month, .weekOfMonth], from: date)
newDateComponents.weekOfMonth = newDateComponents.weekOfMonth! - 1
newDateComponents.weekday = 2
return calendar.date(from: newDateComponents)
}
Therefore, the following Playground code shows how to get the first day of the previous week from July 15, 2010:
import Foundation
func firstDayOfPreviousWeek(from date: Date) -> Date? {
let calendar = Calendar(identifier: .gregorian)
var newDateComponents = calendar.dateComponents([.year, .month, .weekOfMonth], from: date)
newDateComponents.weekOfMonth = newDateComponents.weekOfMonth! - 1
newDateComponents.weekday = 2
newDateComponents.timeZone = TimeZone(abbreviation: "GMT")
return calendar.date(from: newDateComponents)
}
let calendar = Calendar(identifier: .gregorian)
var july15Components = DateComponents()
july15Components.day = 15
july15Components.month = 7
july15Components.year = 2010
july15Components.timeZone = TimeZone(abbreviation: "GMT")
let july15 = calendar.date(from: july15Components)!
print(july15) // prints 2010-07-15 00:00:00 +0000
let newDate = firstDayOfPreviousWeek(from: july15)
print(newDate ?? "Could not retrieve a date") // prints 2010-07-05 00:00:00 +0000
This is something I found myself spending hours to figure out and therefore want to share with you.
The question was: How do I determine the day of the year for a specific date?
e.g. January 15 is the 15th day and December 31 is the 365th day when it's not leap year.
Try this:
NSCalendar *gregorian =
[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSUInteger dayOfYear =
[gregorian ordinalityOfUnit:NSDayCalendarUnit
inUnit:NSYearCalendarUnit forDate:[NSDate date]];
[gregorian release];
return dayOfYear;
where date is the date you want to determine the day of the year for. The documentation on the NSCalendar.ordinalityOfUnit method is here.
So the solution I came up with is pretty neat and simple and not in any way complicated.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"D"];
NSUInteger dayOfYear = [[formatter stringFromDate:[NSDate date]] intValue];
[formatter release];
return dayOfYear;
The trick here which I spent so long time to figure out was to use the NSDateFormatter. The "D" is the flag for day of year.
Hope that this was helpful to you who have the same problem as I had.
Using ARC and replacing deprecated symbols, John Feminella's answer would look like this:
NSCalendar *greg = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
NSUInteger dayOfYear= [greg ordinalityOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitYear forDate:[NSDate date]];
Swift 4 variation:
let date = Date() // now
let calendar = Calendar(identifier: .gregorian)
let dayOfYear = calendar.ordinality(of: .day, in: .year, for: Date())
for Apple Swift version 5.4.2
let formatter = DateFormatter()
formatter.dateFormat = "d"
let day = formatter.string(from: Date())
if let dd = Int(day){
print("day: \(dd)")
}
With the NSDate class. Use message timeIntervalSinceDate. It will return you a NSTimeInterval value (actually it's a double) that represents the seconds elapsed since the date you want. After that, it's easy to convert seconds-to-days.
If you truncate seconds/86400 will give you days.
Let's say you want days since January 1st 2010.
// current date/time
NSDate *now = [[NSData alloc] init];
// seconds elapsed since January 1st 2010 00:00:00 (GMT -4) until now
NSInteval interval = [now timeIntervalSinceDate: [NSDate dateWithString:#"2010-01-01 00:00:00 -0400"]];
// days since January 1st 2010 00:00:00 (GMT -4) until now
int days = (int)interval/86400;
What is the most efficient/recommended way of comparing two NSDates? I would like to be able to see if both dates are on the same day, irrespective of the time and have started writing some code that uses the timeIntervalSinceDate: method within the NSDate class and gets the integer of this value divided by the number of seconds in a day. This seems long winded and I feel like I am missing something obvious.
The code I am trying to fix is:
if (!([key compare:todaysDate] == NSOrderedDescending))
{
todaysDateSection = [eventSectionsArray count] - 1;
}
where key and todaysDate are NSDate objects and todaysDate is creating using:
NSDate *todaysDate = [[NSDate alloc] init];
Regards
Dave
I'm surprised that no other answers have this option for getting the "beginning of day" date for the objects:
[[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay startDate:&date1 interval:NULL forDate:date1];
[[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay startDate:&date2 interval:NULL forDate:date2];
Which sets date1 and date2 to the beginning of their respective days. If they are equal, they are on the same day.
Or this option:
NSUInteger day1 = [[NSCalendar currentCalendar] ordinalityOfUnit:NSDayCalendarUnit inUnit: forDate:date1];
NSUInteger day2 = [[NSCalendar currentCalendar] ordinalityOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitEra forDate:date2];
Which sets day1 and day2 to somewhat arbitrary values that can be compared. If they are equal, they are on the same day.
You set the time in the date to 00:00:00 before doing the comparison:
unsigned int flags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* components = [calendar components:flags fromDate:date];
NSDate* dateOnly = [calendar dateFromComponents:components];
// ... necessary cleanup
Then you can compare the date values. See the overview in reference documentation.
There's a new method that was introduced to NSCalendar with iOS 8 that makes this much easier.
- (NSComparisonResult)compareDate:(NSDate *)date1 toDate:(NSDate *)date2 toUnitGranularity:(NSCalendarUnit)unit NS_AVAILABLE(10_9, 8_0);
You set the granularity to the unit(s) that matter. This disregards all other units and limits comparison to the ones selected.
For iOS8 and later, checking if two dates occur on the same day is as simple as:
[[NSCalendar currentCalendar] isDate:date1 inSameDayAsDate:date2]
See documentation
This is a shorthand of all the answers:
NSInteger interval = [[[NSCalendar currentCalendar] components: NSDayCalendarUnit
fromDate: date1
toDate: date2
options: 0] day];
if(interval<0){
//date1<date2
}else if (interval>0){
//date2<date1
}else{
//date1=date2
}
I used the Duncan C approach, I have fixed some mistakes he made
-(NSInteger) daysBetweenDate:(NSDate *)firstDate andDate:(NSDate *)secondDate {
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDateComponents *components = [currentCalendar components: NSDayCalendarUnit fromDate: firstDate toDate: secondDate options: 0];
NSInteger days = [components day];
return days;
}
I use this little util method:
-(NSDate*)normalizedDateWithDate:(NSDate*)date
{
NSDateComponents* components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
fromDate: date];
return [calendar_ dateFromComponents:components]; // NB calendar_ must be initialized
}
(You obviously need to have an ivar called calendar_ containing an NSCalendar.)
Using this, it is easy to check if a date is today like this:
[[self normalizeDate:aDate] isEqualToDate:[self normalizeDate:[NSDate date]]];
([NSDate date] returns the current date and time.)
This is of course very similar to what Gregory suggests. The drawback of this approach is that it tends to create lots of temporary NSDate objects. If you're going to process a lot of dates, I would recommend using some other method, such as comparing the components directly, or working with NSDateComponents objects instead of NSDates.
The answer is simpler than everybody makes it out to be. NSCalendar has a method
components:fromDate:toDate:options
That method lets you calculate the difference between two dates using whatever units you want.
So write a method like this:
-(NSInteger) daysBetweenDate: (NSDate *firstDate) andDate: (NSDate *secondDate)
{
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDateComponents components* = [currentCalendar components: NSDayCalendarUnit
fromDate: firstDate
toDate: secondDate
options: 0];
NSInteger days = [components days];
return days;
}
If the above method returns zero, the two dates are on the same day.
From iOS 8.0 onwards, you can use:
NSCalendar *calendar = [NSCalendar currentCalendar];
NSComparisonResult dateComparison = [calendar compareDate:[NSDate date] toDate:otherNSDate toUnitGranularity:NSCalendarUnitDay];
If the result is e.g. NSOrderedDescending, otherDate is before [NSDate date] in terms of days.
I do not see this method in the NSCalendar documentation but it is in the iOS 7.1 to iOS 8.0 API Differences
For developers coding in Swift 3
if(NSCalendar.current.isDate(selectedDate, inSameDayAs: NSDate() as Date)){
// Do something
}
With Swift 3, according to your needs, you can choose one of the two following patterns in order to solve your problem.
#1. Using compare(_:to:toGranularity:) method
Calendar has a method called compare(_:to:toGranularity:). compare(_:to:toGranularity:) has the following declaration:
func compare(_ date1: Date, to date2: Date, toGranularity component: Calendar.Component) -> ComparisonResult
Compares the given dates down to the given component, reporting them orderedSame if they are the same in the given component and all larger components, otherwise either orderedAscending or orderedDescending.
The Playground code below shows hot to use it:
import Foundation
let calendar = Calendar.current
let date1 = Date() // "Mar 31, 2017, 2:01 PM"
let date2 = calendar.date(byAdding: .day, value: -1, to: date1)! // "Mar 30, 2017, 2:01 PM"
let date3 = calendar.date(byAdding: .hour, value: 1, to: date1)! // "Mar 31, 2017, 3:01 PM"
/* Compare date1 and date2 */
do {
let comparisonResult = calendar.compare(date1, to: date2, toGranularity: .day)
switch comparisonResult {
case ComparisonResult.orderedSame:
print("Same day")
default:
print("Not the same day")
}
// Prints: "Not the same day"
}
/* Compare date1 and date3 */
do {
let comparisonResult = calendar.compare(date1, to: date3, toGranularity: .day)
if case ComparisonResult.orderedSame = comparisonResult {
print("Same day")
} else {
print("Not the same day")
}
// Prints: "Same day"
}
#2. Using dateComponents(_:from:to:)
Calendar has a method called dateComponents(_:from:to:). dateComponents(_:from:to:) has the following declaration:
func dateComponents(_ components: Set<Calendar.Component>, from start: Date, to end: Date) -> DateComponents
Returns the difference between two dates.
The Playground code below shows hot to use it:
import Foundation
let calendar = Calendar.current
let date1 = Date() // "Mar 31, 2017, 2:01 PM"
let date2 = calendar.date(byAdding: .day, value: -1, to: date1)! // "Mar 30, 2017, 2:01 PM"
let date3 = calendar.date(byAdding: .hour, value: 1, to: date1)! // "Mar 31, 2017, 3:01 PM"
/* Compare date1 and date2 */
do {
let dateComponents = calendar.dateComponents([.day], from: date1, to: date2)
switch dateComponents.day {
case let value? where value < 0:
print("date2 is before date1")
case let value? where value > 0:
print("date2 is after date1")
case let value? where value == 0:
print("date2 equals date1")
default:
print("Could not compare dates")
}
// Prints: date2 is before date1
}
/* Compare date1 and date3 */
do {
let dateComponents = calendar.dateComponents([.day], from: date1, to: date3)
switch dateComponents.day {
case let value? where value < 0:
print("date2 is before date1")
case let value? where value > 0:
print("date2 is after date1")
case let value? where value == 0:
print("date2 equals date1")
default:
print("Could not compare dates")
}
// Prints: date2 equals date1
}
int interval = (int)[firstTime timeIntervalSinceDate:secondTime]/(60*60*24);
if (interval!=0){
//not the same day;
}
my solution was two conversions with NSDateFormatter:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyyMMdd"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
NSDate *today = [NSDate dateWithTimeIntervalSinceNow:0];
NSString *todayString=[dateFormat stringFromDate:today];
NSDate *todayWithoutHour=[dateFormat dateFromString:todayString];
if ([today compare:exprDate] == NSOrderedDescending)
{
//do
}
The documentation regarding NSDate indicates that the compare: and isEqual: methods will both perform a basic comparison and order the results, albeit they still factor in time.
Probably the simplest way to manage the task would be to create a new isToday method to the effect of the following:
- (bool)isToday:(NSDate *)otherDate
{
currentTime = [however current time is retrieved]; // Pardon the bit of pseudo-code
if (currentTime < [otherDate timeIntervalSinceNow])
{
return YES;
}
else
{
return NO;
}
}
This is a particularly ugly cat to skin, but here's another way to do it. I don't say it's elegant, but it's probably as close as you can get with the date/time support in iOS.
bool isToday = [[NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterFullStyle timeStyle:NSDateFormatterNoStyle] isEqualToString:[NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterFullStyle timeStyle:NSDateFormatterNoStyle]];
NSUInteger unit = NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit;
NSDateComponents *comp = [cal components:unit
fromDate:nowDate
toDate:setDate
options:0];
NSString *dMonth;
dMonth = [NSString stringWithFormat:#"%02ld",comp.month];
NSString *dDay;
dDay = [NSString stringWithFormat:#"%02ld",comp.day + (comp.hour > 0 ? 1 : 0)];
compare hour as well to fix 1day difference