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().
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.
What would be the best algorithm / code to convert time to float? I'm doing this in Obj-c but really all I need is an algorithm and I can make the code from there.
EX:
1:30am to 1.50
2:15pm to 14.25
5:45pm to 17.75
EDIT:
My time format is minutes since midnight.
You can parse the string to get two int numbers and an am/pm part (h, m, and a), and then calculate the fraction as follows:
int h, m, a;
// set a to 0 for "am", or 1 for "pm"
float time = 60*(a*12+h)+m;
float res = time / 60.0;
How about NSDate timeIntervalSinceDate? And divide by 3600, of course.
Or, for time within a single day, use NSDateFormatter with an "A" date format to produce milliseconds since midnight, then mash on that.
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 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).
To be more precise this is not working for me:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"YYYY-MM-ddTHH:mm:ssZ"];
NSDate *prevday = [formatter dateFromString:#"2011-04-07T22:00:48Z"];
prevday is returning NIL.
You need to inserts ticks in the string:
#"YYYY'-'MM'-'dd'T'HH':'mm':'ss'Z'"
According to this document;
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html%23//apple_ref/doc/uid/TP40002369-SW1
Date formatters use the following version of the standard;
http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns
so you'd use the date format 'yyyy-MM-ddTHH:mm:ss' I'm not sure about the Z, is that supposed to be the time zone? Z in the format string will give you the 3 letter time zone name.
Edited based on your edit above:
[formatter setDateFormat:#"YYYY-MM-ddTHH:mm:ssZ"];
Is why it's returning nil.
The 'Z' expects the time zone to be in the 3 character time zone thing...
Drop the Z or escape it with a quote character. Read the tr35 doc above for more info.