How to compare two times in iOS - cocoa-touch

I am trying to compare two times in iOS. I have list of times in my array i just want to check current time is matching to the any one the array value. can any help how to do that. i searched whole internet i did't get any idea.
I saw this question answer but i can't get a exact result.

According to Apple documentation of NSDate compare:
Returns an NSComparisonResult value that indicates the temporal ordering of the receiver and another given date.
- (NSComparisonResult)compare:(NSDate *)anotherDate
Parameters anotherDate
The date with which to compare the receiver. This value must not be nil.
Return Value
If:
The receiver and anotherDate are exactly equal to each other, NSOrderedSame
The receiver is later in time than anotherDate, NSOrderedDescending
The receiver is earlier in time than anotherDate, NSOrderedAscending
In other words:
if ([date1 compare:date2]==NSOrderedSame) ...
Note that it might be easier to read and write this :
if ([date2 isEqualToDate:date2]) ...

-(NSString *)determineDateFromstring:(long long)date
{
NSTimeInterval interval=date;
NSDate *currentTime=[NSDate dateWithTimeIntervalSince1970:interval/1000];
NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_GB"];
NSNumber *myDateInString=[NSNumber numberWithLongLong:[[NSDate date] timeIntervalSince1970]*1000];
NSTimeInterval inte=[myDateInString longLongValue];
NSDate *todayTime=[NSDate dateWithTimeIntervalSince1970:inte/1000];
NSCalendar *currentcalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [currentcalendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:todayTime];
int currentday=[components day];
int currentyear=[components year];
NSCalendar *paramtercalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *parametercomponents = [paramtercalendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:currentTime];
int currentday1=[parametercomponents day];
int currentyear1=[parametercomponents year];
if (currentday==currentday1) {
Nslog(date are same);
}else if(currentyear==currentyear1) {
Nslog(year are same);
}else{
}
}
if u want to check two date are equal or not then u can compare NStimeIntervals(long long values). if same then time ,date and year are same otherwise different.
I hope this answer will be the right to your question.......

Why don't you check the interval since 1970 and compare the integer value..
long long int i = [[NSDate date] timeIntervalSince1970];
I hope this will help.Also if you have values in form of NSString then you first need to convert them to NSDate
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm:ssSSSSSS";
NSDate *d = [dateFormatter dateFromString:#"your date string"];

Related

Get number of days between two NSDate dates in a particular timezone

I found the codes to calculate days difference between two dates here.
I write a method :
-(NSInteger)daysWithinEraFromDate:(NSDate *) startDate toDate:(NSDate *) endDate
{
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSInteger startDay=[gregorian ordinalityOfUnit:NSDayCalendarUnit
inUnit: NSEraCalendarUnit forDate:startDate];
NSInteger endDay=[gregorian ordinalityOfUnit:NSDayCalendarUnit
inUnit: NSEraCalendarUnit forDate:endDate];
return endDay-startDay;
}
This method has a problem: it can't consider the timezone thing. Even I add a line like this:
[gregorian setTimeZone:[NSTimeZone localTimeZone]];
My test code is like this:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *strDate = #"2012-09-03 23:00:00";
NSDate *dateStart = [dateFormat dateFromString:strDate];
strDate = #"2012-09-04 01:00:00";
NSDate *dateEnd = [dateFormat dateFromString:strDate];
NSLog(#"Days difference between %# and %# is: %d days",[dateFormat stringFromDate:dateStart],[dateFormat stringFromDate:dateEnd],[self daysWithinEraFromDate:dateStart toDate:dateEnd]);
The result is:
Days difference between 2012-09-03 23:00:00 and 2012-09-04 01:00:00 is: 0 days
I want to get 1 day as result by the number of midnights between the two dates. My timezone is GMT +8. But this calculation is based on GMT, so I get the wrong days number. Is there anyway to solve this problem? Thank you.
Scott Lemmon's method can solve my problem. I rewrite my code like this:
-(NSInteger)daysWithinEraFromDate:(NSDate *) startDate toDate:(NSDate *) endDate
{
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setTimeZone:[NSTimeZone localTimeZone]];
NSDate *newDate1 = [startDate dateByAddingTimeInterval:[[NSTimeZone localTimeZone] secondsFromGMT]];
NSDate *newDate2 = [endDate dateByAddingTimeInterval:[[NSTimeZone localTimeZone] secondsFromGMT]];
NSInteger startDay=[gregorian ordinalityOfUnit:NSDayCalendarUnit
inUnit: NSEraCalendarUnit forDate:newDate1];
NSInteger endDay=[gregorian ordinalityOfUnit:NSDayCalendarUnit
inUnit: NSEraCalendarUnit forDate:newDate2];
return endDay-startDay;
}
If the time zone offset isn't working, how about just add or subtract it manually instead?
In your case NSDate *newDate = [oldDate dateByAddingTimeInterval:(-8 * 60 * 60)]; to subtract off your +8 hours.
Or if you want to find the GMT offset automatically as well then it would simply be NSDate *newDate = [oldDate dateByAddingTimeInterval:(-[[NSTimeZone localTimeZone] secondsFromGMT])
Another thought:
A perhaps easier solution would be to just disregard the time information altogether. Just set it to the same arbitrary number for both dates, then as long as the dates come from the same timezone you will always get the correct number of mid-nights between them, regardless of GMT offset.
What you really want is the NSDate method timeIntervalSinceDate:, and take that result and if it's more than 0 but less than 86400 (the number of seconds in a day), that's one day. Otherwise, divide your result by 86400 and you'll get the number of days.
The way you currently have your code, there's only 2 hours between the two days and that's why you are seeing a result of 0 and not one.
Edit - and to determine if midnight has happened, let's try this function I just wrote off the top of my head:
- (NSDate *) getMidnightDateFromDate: (NSDate *) originalDate
{
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSIntegerMax fromDate:originalDate];
[components setHour:0];
[components setMinute:0];
[components setSecond:0];
NSDate *midnight = [[NSCalendar currentCalendar] dateFromComponents:components];
return(midnight);
}
- (BOOL) howManyDaysDifferenceBetween: startDate and: endDate
{
NSDate * firstMidnight = [self getMidnightDateFromDate: startDate];
NSDate * secondMidnight = [self getMidnightDateFromDate: endDate];
NSTimeInterval timeBetween = [firstMidnight timeIntervalSinceDate: secondMidnight];
NSInteger numberOfDays = (timeBetween / 86400);
return(numberOfDays);
}
which I'm basing off Dave Delong's answer to this question. No guarantees that my code will work (I didn't test it), but I think the concept is sound.

Date comparison between two NSDate

This is my first time comparing dates in Objective-C. I've been searching the web for a while and all the examples I found involve building a NSDate from a string so I decided to ask a new question here...
My question is as follows:
I need to know if two NSDates are in the same day, ignoring the time. I got two NSArray's containing a set of dates and one by one I need to determine which one from the first NSArray is in the same day as in the second array.
- (void)setActiveDaysColor:(UIColor *)activeDaysColor
{
for (DayView *actualDay in _days)
{
NSDate *actualDayDate = [actualDay date];
for (NSDate *activeDayDate in self.dates)
{
// Comparison code
// If both dates are the same, tint actualDay with a different color
}
}
}
Thank you in advance and have a nice day.
Alex.
create new dates by omitting the time components. and use one of the compare methods
earlierDate:
laterDate:
compare:
isEqualToDate
example
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger components = (NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit);
NSDateComponents *firstComponents = [calendar components:components fromDate:firstDate];
NSDateComponents *secondComponents = [calendar components:components fromDate:secondDate];
NSDate *date1 = [calendar dateFromComponents:firstComponents];
NSDate *date2 = [calendar dateFromComponents:secondComponents];
NSComparisonResult result = [date1 compare:date2];
if (result == NSOrderedAscending) {
} else if (result == NSOrderedDescending) {
} else {
}
-(BOOL)isSameDay:(NSDate*)date1 otherDay:(NSDate*)date2 {
NSCalendar* calendar = [NSCalendar currentCalendar];
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents* comp1 = [calendar components:unitFlags fromDate:date1];
NSDateComponents* comp2 = [calendar components:unitFlags fromDate:date2];
return [comp1 day] == [comp2 day] &&
[comp1 month] == [comp2 month] &&
[comp1 year] == [comp2 year];}
Check NSDate documentation, These are the methods to compare date
isEqualToDate
earlierDate
laterDate
compare
in your case
if([actualDayDate isEqualToDate:activeDayDate])
{
}
Thanks for all your answers.
I found a cleaner answer to my question answered in a totally unrelated post but that actually works perfectly.
if ((unsigned int)[actualDayDate timeIntervalSinceDate:activeDayDate] / 60 / 60 / 24 == 0)
{
// do Stuff
}
Instead of the loops that you have in your code you could use a predicate to filter out all the objects that are today. Filtering out what dates are today is done by comparing it to the beginning of today and the end of today.
You can set any NSDate to the beginning of that date like this (see this answer)
NSDate *beginDate = [NSDate date];
[[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit startDate:&beginDate interval:NULL forDate:beginDate];
Then to get the end date you simply add one day to it. Don't add days by calculating the number of seconds. This won't work with daylight savings! Do it like this (also see this answer):
NSDateComponents *oneDay = [[NSDateComponents alloc] init];
[oneDay setDay:1];
// one day after begin date
NSDate *endDate = [[NSCalendar currentCalendar] dateByAddingComponents:oneDay toDate:beginDate options:0];
Now that you have the two dates that define the range for today you can filter all your DayViews using a NSPredicate to get a new array of all the DayViews who's day is today, like this (see this answer):
// filter DayViews to only include those where the day is today
NSArray *daysThatAreToday = [_days filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"(date >= %#) AND (date <= %#)", beginDate, endDate]];
Now you can apply the tint color to all the DayViews by enumerating the new array (that contains todays DayViews)
[daysThatAreToday enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
// Set the tint color here...
}];
This, in my opinion, is a clean but more importantly a correct way of solving your problem. It reads clearly and handles daylight savings and other calendars then gregorian. It can also easily be reused if you want to tint all the DayViews for a certain week (or any other time period).

iOS: Compare two dates

I have a NSDate that I must compare with other two NSDate and I try with NSOrderAscending and NSOrderDescending but if my date is equal at other two dates?
Example: if I have a myDate = 24/05/2011 and other two that are one = 24/05/2011 and two 24/05/2011 what can I use?
According to Apple documentation of NSDate compare:
Returns an NSComparisonResult value that indicates the temporal ordering of the receiver and another given date.
- (NSComparisonResult)compare:(NSDate *)anotherDate
Parameters anotherDate
The date with which to compare the
receiver. This value must not be nil.
If the value is nil, the behavior is
undefined and may change in future
versions of Mac OS X.
Return Value
If:
The receiver and anotherDate are
exactly equal to each other,
NSOrderedSame
The receiver is later in
time than anotherDate,
NSOrderedDescending
The receiver is
earlier in time than anotherDate,
NSOrderedAscending
In other words:
if ([date1 compare:date2] == NSOrderedSame) ...
Note that it might be easier in your particular case to read and write this :
if ([date2 isEqualToDate:date2]) ...
See Apple Documentation about this one.
After searching, I've got to conclusion that the best way of doing it is like this:
- (BOOL)isEndDateIsSmallerThanCurrent:(NSDate *)checkEndDate
{
NSDate* enddate = checkEndDate;
NSDate* currentdate = [NSDate date];
NSTimeInterval distanceBetweenDates = [enddate timeIntervalSinceDate:currentdate];
double secondsInMinute = 60;
NSInteger secondsBetweenDates = distanceBetweenDates / secondsInMinute;
if (secondsBetweenDates == 0)
return YES;
else if (secondsBetweenDates < 0)
return YES;
else
return NO;
}
You can change it to difference between hours also.
If you want to compare date with format of dd/MM/yyyy only, you need to add below lines between NSDate* currentdate = [NSDate date]; && NSTimeInterval distance
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]
autorelease]];
NSString *stringDate = [dateFormatter stringFromDate:[NSDate date]];
currentdate = [dateFormatter dateFromString:stringDate];
I take it you are asking what the return value is in the comparison function.
If the dates are equal then returning NSOrderedSame
If ascending ( 2nd arg > 1st arg ) return NSOrderedAscending
If descending ( 2nd arg < 1st arg ) return NSOrderedDescending
I don't know exactly if you have asked this but if you only want to compare the date component of a NSDate you have to use NSCalendar and NSDateComponents to remove the time component.
Something like this should work as a category for NSDate:
- (NSComparisonResult)compareDateOnly:(NSDate *)otherDate {
NSUInteger dateFlags = NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit;
NSCalendar *gregorianCalendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *selfComponents = [gregorianCalendar components:dateFlags fromDate:self];
NSDate *selfDateOnly = [gregorianCalendar dateFromComponents:selfComponents];
NSDateComponents *otherCompents = [gregorianCalendar components:dateFlags fromDate:otherDate];
NSDate *otherDateOnly = [gregorianCalendar dateFromComponents:otherCompents];
return [selfDateOnly compare:otherDateOnly];
}
NSDate actually represents a time interval in seconds since a reference date (1st Jan 2000 UTC I think). Internally, a double precision floating point number is used so two arbitrary dates are highly unlikely to compare equal even if they are on the same day. If you want to see if a particular date falls on a particular day, you probably need to use NSDateComponents. e.g.
NSDateComponents* dateComponents = [[NSDateComponents alloc] init];
[dateComponents setYear: 2011];
[dateComponents setMonth: 5];
[dateComponents setDay: 24];
/*
* Construct two dates that bracket the day you are checking.
* Use the user's current calendar. I think this takes care of things like daylight saving time.
*/
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDate* startOfDate = [calendar dateFromComponents: dateComponents];
NSDateComponents* oneDay = [[NSDateComponents alloc] init];
[oneDay setDay: 1];
NSDate* endOfDate = [calendar dateByAddingComponents: oneDay toDate: startOfDate options: 0];
/*
* Compare the date with the start of the day and the end of the day.
*/
NSComparisonResult startCompare = [startOfDate compare: myDate];
NSComparisonResult endCompare = [endOfDate compare: myDate];
if (startCompare != NSOrderedDescending && endCompare == NSOrderedDescending)
{
// we are on the right date
}
Check the following Function for date comparison first of all create two NSDate objects and pass to the function:
Add the bellow lines of code in viewDidload or according to your scenario.
-(void)testDateComaparFunc{
NSString *getTokon_Time1 = #"2016-05-31 03:19:05 +0000";
NSString *getTokon_Time2 = #"2016-05-31 03:18:05 +0000";
NSDateFormatter *dateFormatter=[NSDateFormatter new];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss Z"];
NSDate *tokonExpireDate1=[dateFormatter dateFromString:getTokon_Time1];
NSDate *tokonExpireDate2=[dateFormatter dateFromString:getTokon_Time2];
BOOL isTokonValid = [self dateComparision:tokonExpireDate1 andDate2:tokonExpireDate2];}
here is the function
-(BOOL)dateComparision:(NSDate*)date1 andDate2:(NSDate*)date2{
BOOL isTokonValid;
if ([date1 compare:date2] == NSOrderedDescending) {
//"date1 is later than date2
isTokonValid = YES;
} else if ([date1 compare:date2] == NSOrderedAscending) {
//date1 is earlier than date2
isTokonValid = NO;
} else {
//dates are the same
isTokonValid = NO;
}
return isTokonValid;}
Simply change the date and test above function :)

iOS: problem with NSDate and NSDateComponent

NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setTimeZone:[ NSTimeZone timeZoneForSecondsFromGMT:(+0*3600) ] ] ;
[components setYear:2011];
[components setDay:13];
[components setMonth:5];
NSDate *date1 = [gregorianCalendar dateFromComponents:components];
NSDate *date2 = [[NSDate alloc] init];
NSTimeInterval diff = [data2 timeIntervalSinceDate:date1];
NSString *intervalString = [NSString stringWithFormat:#"%f", diff];
int second = [intervalString intValue];
int period = second/3600/24;
NSLog(#"period:%d", period);
NSLog(#"date1:%#", data1);
NSLog(#"date2:%#", data2);
In consol the result is:
2011-05-12 10:57:00.406 Project[297:707] period:0;
2011-05-12 10:57:00.375 Project[297:707] data2:2011-05-12 08:56:52 +0000
2011-05-12 10:57:00.402 Project[297:707] data1:2011-05-13 00:00:00 +0000
I don't understand why period is "0", it must be "1"; Can you help me?
NSTimeInterval is just a double, so you shouldn't need to convert it to a string then back to an int.
What happens if you do something like this:
NSTimeInterval diff = [data2 timeIntervalSinceDate:date1];
int period = (int)diff/3600/24;
NSLog(#"period:%d", period);
Also if the interval is diff is less than 3600*24 the result of diff/3600/24 will be less than 1, so the int value will be flattened to 0.
date2 is the current date. No idea why period should be one here.
At the time of this writing, diff is approx. -53000. Dividing it brings it to zero as you get and is expected.
Also keep in mind that NSTimeInterval is a floating point number, and you are converting it through a string to an integer, which will drop off the fractions.
And there is no point in doing this conversion through a string - just use int seconds = (int) diff;.
Please copy/paste your code directly, as you refer to data1and data2, but your variables are called date1 and date2. But then, this might already be your bug, if those variables are declared elsewhere.

Objective C - How can i get the weekday from NSDate?

I need to be able to get the weekday from nsdate, i have the following code and it always return 1. I tried to change the month, i tried everything from 1 to 12 but the result of the week day is always 1.
NSDate *date2 = [[NSDate alloc] initWithString:[NSString stringWithFormat:#"%d-%d-%d", 2010, 6, 1]];
unsigned units2 = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit;
NSCalendar *calendar2 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components2 = [calendar2 components:units2 fromDate:date2];
int startWeekDay = [components2 weekday];
[date2 release];
[calendar2 release];
Creating an NSDateFormatter that only contains the weekday will do what you want.
NSDate *now = [NSDate date];
NSDateFormatter *weekday = [[[NSDateFormatter alloc] init] autorelease];
[weekday setDateFormat: #"EEEE"];
NSLog(#"The day of the week is: %#", [weekday stringFromDate:now]);
If you need internationalization, the NSDateFormatter can be sent a locale, which will give the proper translation.
The date formatters are controlled through this standard: Unicode Date Formats
Edit for Mac:
The format of the string has to be —YYYY-MM-DD HH:MM:SS ±HHMM according to the docs, all fields mandatory
Old answer (for iPhone and tested on simulator):
There is no (public) -initWithString: method in NSDate, and what you get returned is not what you expect.
Use a properly configured (you need to give the input format) NSDateFormatter and -dateFromString:.
I solved the problem, The problem was that the format of my date string was wrong:
NSDate *date = [[NSDate alloc] initWithString:[NSString stringWithFormat:#"%d-%d-%d 10:45:32 +0600", selectedYear, selectedMonth, selectedDay]];
and since i don't care about the time i randomly pass a time to the end of the string
I'm using Xcode 6.4
There are many ways to setup an NSDate. I won't go over that here. You've done it one way, using a string (A method I avoid due to it being very vulnerable to error), and I'm setting it another way. Regardless, access your components weekday or setDay methods.
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
[calendar setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en-US"]];
NSDateComponents *components = [calendar components:(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitWeekday|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond) fromDate:[NSDate date]];
NSDate *date = [calendar dateFromComponents:components];
this way, you can get or set any of these components like this:
[components weekday] //to get, then doSomething
[components setDay:6]; //to set
and, of course, set NSDate *date = [calendar dateFromComponents:components];only after you've changed the components.
I added in the extra local and other components for context in case you'd like to set those too.
It's free code, don't knock it.
Anyone coming here looking for a more recent Swift 4 solution:
extension Date {
func getDayOfWeek() -> String {
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone.current
dateFormatter.locale = Locale(identifier: "en_US")
dateFormatter.setLocalizedDateFormatFromTemplate("EEEE")
return dateFormatter.string(from: self)
}
}
let inputString = "2018-04-16T15:47:39.000Z"
let result = inputString.getDayOfWeek()
result = Monday
Most upvoting answer in Swift 3.
let formatter = DateFormatter()
formatter.dateFormat = "EEEE"
print("The day of the week is \(formatter.string(from: Date()))")
I used the following website which helped with setting up the string to retrieve the format I wanted on my date
Coder Wall - Guide to objective c date formatting