//Gets the time right now
NSDate *now = [NSDate date];
//Stores the difference in seconds between when the test was started and now.
NSTimeInterval interval = [self.expires timeIntervalSinceDate:now];
if (interval == 0) {
...
}
Any reason why the condition wont be true?
Thanks.
Since NSTimeInterval is only a double, you might have floating point rounding error. Try using something like
if (abs(interval) < EPS) {
where EPS is a small enough constant.
Or, if you want to know seconds and not milliseconds, you can truncate that double to int.
But judging from your code, I think you might want to check if timeline has already expired, not that it expires at this exact moment. Probability of the later is very small. This should do the trick.
if (interval < 0) {
Related
I have a core data table view and I am comparing dates. The method which I currently use is: if ([todayDate compare: [NSDate date]]==NSOrderedAscending) . This works perfectly but slow. I do not need to know the difference in time though. Any help is much appreciated!
I really think, that NSDates method isEqualToDate: is what you are searching for. Seems to me to be the Apple-way to answer your question:
NSDate *date1 = ...;
NSDate *date2 = ...;
BOOL datesAreEqual = [date1 isEqualToDate:date2];
For more information visit https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsdate_Class/Reference/Reference.html
One option could be to not actually create a new NSDate object but use the time interval for comparison. Don't know about the performance, but it might be worth a try.
if ([todayDate timeIntervalSinceReferenceDate] > [NSDate timeIntervalSinceReferenceDate]) {
...
}
You should remember the current date or current timestamp in a local variable:
NSTimeInterval current = [NSDate timeIntervalSinceReferenceDate];
And use this value later for all your comparisons:
myTimestamp = [myDate timeIntervalSinceReferenceDate]
if (myTimestamp == current) {
return NSOrderedSame;
} else if (myTimestamp > current) {
return NSOrderedDescending;
} else {
return NSOrderedAscending;
}
Or a faster way, using C functions:
// Get the current calendar time as a time_t object.
time_t time ( time_t * timer );
// Return difference between two times
double difftime ( time_t time2, time_t time1 );
I've not measured it, but you may want to try:
NSTimeInterval i = [todayDate timeIntervalSinceNow];
where the result (i) may be positive or negative.
You might also try CFDateCompare.
Or you might want to consider another way to represent a point in time in your database -- such as a CFTimeInterval (a double representing the number of seconds from a common reference time).
How to find one text field value is within past 60 day excluding current date.
For example if I enter value in text field is 20-July-2012 using Date Picker.Then I click submit,it'll check that specific is date is within 60 days or not. If the values are entered which is before 60 days an alert message is displayed. The values are retrieved from api.
NSDate *today = [NSDate date];
NSTimeInterval dateTime;
if ([pickerDate isEqualToDate:today]) //pickerDate is a NSDate
{
NSLog (#"Dates are equal");
}
dateTime = ([pickerDate timeIntervalSinceDate:today] / 86400);
if(dateTime < 0) //Check if visit date is a past date, dateTime returns - val
{
NSLog (#"Past Date");
}
else
{
NSLog (#"Future Date");
}
Change the value of 86400 to suit your query.In this case, it is the number of seconds we want to compare.
First, convert the text into an NSDate. Then use
timeIntervalSinceDate:[NSDate dateWithTimeIntervalSinceNow:0]
There are a couple of ways to convert text into an NSDate. You can format the text correctly and then use dateWithString or you can convert everything into numbers, multiply them out, and one of the dateWithTimeInterval methods.
If you want the user to be able to enter "July" (plain text month) then you might want to write a method that converts months into their numerical equivalents with string matching.
NSDate *lastDate; //your date I hope you have created it
NSDate *todaysDate = [NSDate date];
NSTimeInterval lastDiff = [lastDate timeIntervalSinceNow];
NSTimeInterval todaysDiff = [todaysDate timeIntervalSinceNow];
NSTimeInterval dateDiff = lastDiff - todaysDiff; // number of seconds
int days = dateDiff/(60*60*24); // 5.8 would become 5 as I'm taking int
How do you define 60 days?
You may want to use NSCalendar -dateByAddingComponents:toDate:options: to ensure your 60 days really are 60 days.
NSCalendar also provides -components:fromDate: and -dateFromComponents: which are very nice when dealing with date components.
If 60 days do not need to be true calendar days (daylight saving time switches, astronomical time corrections, stuff like that), you can just have fun with NSDate and the time interval methods alone.
Is there a quick way in Objective-C of identifying NSDate's in an NSArray that have a time of day after a given time (e.g. 8pm)?
I can't quite see anyway other than manually walking through each NSDate in the array and then using NSDateComponents to break out the hour/minute/second...Not even sure if there is a simple way to get the time from an NSDate in a fashion that represents a fraction of 24hours, as this might help a little. (e.g. 6pm would be 18/24 = 0.75 in this case)
There is no need to break in NSDateComponents.
NSTimeInterval interval = [date1 timeIntervalSinceDate:date2];
if (interval > 0) {
// date2 is earlier
} else {
// date1 is earlier
}
Now you can represent your target time(8 P.M., for example) with date2 and compare all dates of array with that.
Haven't tried this myself, but I guess
- (NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate
is what you're looking for.
I'm sure I'm missing something and the answer is very simple, but I can't seem to understand why this is happening. I'm trying to make an average of dates:
NSInteger runningSum =0;
NSInteger count=0;
for (EventoData *event in self.events) {
NSDate *dateFromString = [[NSDate alloc] init];
if (event.date != nil) {
dateFromString = [dateFormatter dateFromString:event.date];
runningSum += (NSInteger)[dateFromString timeIntervalSince1970];
count += 1;
}
}
if (count>0) {
NSLog(#"average is: %#",[NSDate dateWithTimeIntervalSince1970:(NSInteger)((CGFloat)runningAverage/count)]);
}
Everything seems to work OK, except for runningSum += (NSInteger)[dateFromString timeIntervalSince1970], which gives an incorrect result. If I put a breakpoint when taking the average of two equal dates (2009-10-10, for example, which is a timeInterval of 1255125600), runningSum is -1784716096, instead of the expected 2510251200.
I've tried using NSNumber and I get the same result. Can anybody point me in the right direction?
Thanks!
Antonio
Is there some reason you are fudging about with NSInteger?
[NSDate timeIntervalSince1970] returns an NSTimeInterval, which is basically a double. Clean up the code and see if that helps.
NSInteger can hold values up to the INT_MAX limit that equals 2147483647 - so your value overflows the integer types limit - remember that timeInterval is a double type.
You can try to use double type in all your calculations or use -timeIntervalSinceReferenceDate method - it returns interval since 1 January 2001 and you might avoid overflow as well.
If your events object is an array or other type that allows to get its size then you can add time interval values already divided by count - that also may help to avoid overflow:
NSTimeInterval runningSum = 0;
NSInteger count = [self.events count];
for (...){
...
runningSum += [dateFromString timeIntervalSince1970]/count;
}
NSInteger is a 32 bit signed integer on iPhone and is therefore limited to values between −2147483648 and +2147483647.
You may get the desired result by using NSUInteger which is an unsigned 32 bit integer able to contain values between 0 and +4294967295.
You should however pay attention to the number of runs through the loop so you don't wrap the values.
I am developing Alarm Clock.
I want to compare a time now and setTime. Is it possible to compare in minutes only.
My Problem is NSDate will compare in seconds, for example 9:38:50 is not equal to 9:38:00.
how can I compare in minutes?
First, convert them to UNIX timestamp ignoring milliseconds.
NSUInteger time1 = (NSUInteger)[date1 timeIntervalSince1970];
NSUInteger time2 = (NSUInteger)[date2 timeIntervalSince1970];
Ignore seconds.
time1 = time1 - (time1 % 60);
time2 = time2 - (time2 % 60);
Now, you can safely compare them.
if (time1 == time2) {
NSLog(#"Bingo");
}
Use NSCalendar to get the NSDateComponents you're interested in. Then it's easy to compare those.
Though it might be sufficient to check whether the current date is later than the alarm date. If it is and the alarm wasn't stopped, you should probably go off even if the computer momentarily lost power or something.