I want to compare two dates and i m trying to do this by using timeIntervalSinceDate method with the below code :
NSTimeInterval timeinterval;
timeinterval = ([dateTo timeIntervalSinceDate:dateFrom]/86400);
NSLog(#"--- intertval= %d", timeinterval);
But I am always getting 0 difference though the dates are different. I tried many ways But not getting any solution.
Use below
NSLog(#"--- intertval= %f", timeinterval);
NSTimeInterval is a double, not an int. Use the appropriate format specifier (%f or %g).
Related
I have 10 records in database. I want to fetch the record where date == mydate.
For eg, there are 2 records in database, which has date of 502479617.512 (in milliseconds).
I am converting the above date to NSDate using
[NSDate dateWithTimeIntervalSinceReferenceDate:502479617.512].
I am getting 2016-12-03 17:40:17 +0000
I have date parameter '502479617.500' which returns same NSDate 2016-12-03 17:40:17 +0000.
But when i try to fetch from coredata, i am getting 0 objects. It is because of milliseconds in the database. How can i discard milliseconds to fetch the records which has same date, time and seconds.
NSDate properties will be stored as NSDate attributes, unless the "Use scalar properties for primitive data types" checkbox is marked.
In that case, the core data attribute will be stored as a NSTimeInterval (double).
You can do it in either of two ways:
First, you could eliminate the seconds decimals when storing the NSDate. It could be automated by overriding didSet for the property. You could also simply do it wherever you do the conversion you describe.
Alternatively, you could specify an interval of one second when fetching. Your fetch request would then need a predicate of this kind:
NSPredicate(format: "date >= %# && date < %#", aDate, aDate.addingTimeInterval(60))
Objective-C
[NSPredicate predicateWithFormat:#"date >= %# && date < %#",
aDate, [aDate dateByAddingTimeInterval:60]];
Just save it as NSString. Then convert back to double when you need it.
I'm working on a counter, and this is my code to find the time.
NSDate *start = [NSDate date];
NSTimeInterval timeInterval = [start timeIntervalSinceDate:start];
NSLog(#"%f",timeInterval);
This is what it returns over and over again.
2014-03-08 17:59:46.834 Time[67444:303] 0.000000
What is happening with this?
You are using timeIntervalSinceNow on an object that represents a very very close value to "now". Since you print in float, the value is rounded down to lower precision and you get 0.
Are you sure you didn't mean timeIntervalSince1970?
NSTimeInterval is defined as a double. If you need precision above float, try printing as double using %ld. If you need even more precision, look into mach_absolute_time().
This question already has answers here:
NSDate isEqualToDate: not working - does it look at seconds and split seconds?
(3 answers)
Closed 8 years ago.
i compare 2 NSDates which are the same and i get false result.
i cant show how i get this dates because its too long , but i can show what i do :
NSLog(#"this date is:%# , and date we check to equality is:%#",thisDate,dateToFind);
if([thisDate isEqualToDate:dateToFind] )
{
NSLog(#"equal date!"); // not printed!
}
the NSLog show me this :
this date is:2012-09-13 14:23:54 +0000 , and date we check to equality is:2012-09-13 14:23:54 +0000
he doesnt print the NSLog .
why ?
As a few have said it seems to be the fractions of a second that are giving you trouble. The reason for this is that an NSDate is simply an object wrapper around an NSTimeInterval(double) with a value in seconds since the reference date(12AM January 1 2001 GMT).
There are a couple main ways to deal with this. Either check the date to see if it is in a given range, or (more likely based on your question) truncate the fractions of a second completely off.
Truncating seconds from an NSDate is trivial code. You may want to truncate all of the dates that you are storing as you store them for quick comparisons. You can truncate an existing NSDate like this:
NSDate *truncatedDate = [NSDate dateWithTimeIntervalSinceReferenceDate:((NSTimeInterval)lround(originalDateObject.timeIntervalSinceReferenceDate))];
This code is pretty self explanatory. It grabs the date's backing time interval rounds it to an integer casts that back to a time interval and creates a new truncated date.
Once you do this to both dates you can then compare your two truncated dates and they will behave as expected.
Or if you must do something without changing your date data you could simply do:
if (lround(thisDate.timeIntervalSinceReferenceDate) == lround(dateToFind.timeIntervalSinceReferenceDate)){
// If whole seconds are equal, as shown in log, this will execute.
}
NSLog(#"this date is:%# , and date we check to equality is:%#",
thisDate,dateToFind);
Try changing the above with:
NSLog(#"this date is:%f , and date we check to equality is:%f",
[thisDate timeIntervalSince1970],
[dateToFind timeIntervalSince1970]);
I have a project that I need to get the current date/time. From that date/time I need to convert to 6 different timezone's date/time. That is no problem converting the times.
Then from each of the converted date/time I need to see if that date is between two other dates that change every week. I am guessing the times that change everyweek could be an array of dates. The current date/time in that timezone needs to find out which date/time in the array it needs to check itself against. The nearest before and the nearest after. This sounds very confusing just typing this.
Any help pointing me in the right direction would be extremely helpful.
Thankyou
NSDate *date1;
NSDate *date2;
NSDate *date3;
NSTimeInterval firstTimeInterval = [date1 timeIntervalSince1970];
NSTimeInterval secondTimeInterval = [date2 timeIntervalSince1970];
NSTimeInterval thirdTimeInterval = [date3 timeIntervalSince1970];
if (firstTimeInterval<secondTimeInterval && secondTimeInterval<thirdTimeInterval) {
// date2 > date1 and date2 < date3
}
Of course, in my case it would crash since dates have no addresses, but it's just an example... Yours would need to have actual dates in them.
To check if a date is in a specified range just get the unix timestamps and compare them directly like so:
NSDate *lowerLimit = ...
NSDate *upperLimit = ...
NSDate *myDate = ...
BOOL inRange = (lowerLimit.timeIntervalSince1970 <= myDate.timeIntervalSince1970) &&
(upperLimit.timeIntervalSince1970 >= myDate.timeIntervalSince1970);
You could also use NSDate's -compare: method, but I think it's more natural to compare the timestamps.
I think the heading itself says what i want to do?
Stil,let me clarify. I am right now working on an application where i have two dated in a format like :
"YYYY:MM:DD HH:MM:SS"
Using which i need to calculate difference between the both dates in the format :
HH:MM:SS
I searched in the wiki and also tried my luck but in vain.Can anybody help me out?
Thanks in advance.
Here's the steps that I think you'll need to perform:
Parse both the dates into NSDate objects using an NSDateFormatter.
Calculate the difference between the two dates using
NSTimeInterval ti = [laterDate timeIntervalSinceDate:earlierDate];
The ti variable above now contains the number of seconds between the two dates. You can reformat that into hours, seconds and minutes with:
NSUInteger h, m, s;
h = (ti / 3600);
m = ((NSUInteger)(ti / 60)) % 60;
s = ((NSUInteger) ti) % 60;
NSString *str = [NSString stringWithFormat:#"%lu:%02lu:%02lu", h, m, s];
Disclaimer
It is late at night for me so the above calculations for hours, minutes and seconds may not be correct.