I’m trying to get the exact time and date when users select a button, and then again when they deselect it.
I’ve been using NSDate dateWithTimeIntervalSince1970 so far. This gets me miliseconds (floats), which are easy to convert to an NSDate and then to a string. However i’ve noticed several times now that this gives no absolute time with differences of up to 1 or 2 minutes compared to real time on my Iphone 4S.
The reason for using floats with miliseconds here is because it makes it easy to send to different methods allowing it to save to Core Data in an NSNumber and thus allowing easy calculations later on.
I’ve found a post on here suggesting the use of CACurrentMediaTime as such but I have been unsuccesful in converting it to a date and a string of that date:
double CurrentTime = CACurrentMediaTime();
NSDate dateWithTimeIntervalSinceNow
gives the right date except that the clock on the simulator is about 35 to 45 minutes ahead of what it actually is on my mac.
Current conversion goes like this:
NSDate *conversion = [NSDate dateWithTimeIntervalSince1970:startTimeInMiliSeconds];
NSDateFormatter *formatter;
NSString *dateString;
formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd-MM-yyyy HH:mm"];
dateString = [formatter stringFromDate:conversion];
Any ideas/references?
Why don’t you just save
NSDates in Core Data and omit the whole conversion process?NSDates are a natural datatype for Core-Data entries.