Lacrifilm Lacrifilm - 6 months ago 17
Objective-C Question

initWithTimeIntervalSinceReferenceDate return null when the year is less than 2000

I have this code below that storage my date from date picker:

uint64_t offset = (uint64_t)[[self.dataPicker date] timeIntervalSinceReferenceDate];
// store this 64-bit unsigned integer.

NSLog(@"Offset -> %llu",offset);


To convert date format again I use the following command:

NSDate *data = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:offset];
NSLog(@"Before -> %@",data);


But this code has a problem, the command
initWithTimeIntervalSinceReferenceDate
always return null when I select a smaller year than 2000 (1999,1998,1997 ...), but the
offset
is still to store a value.

What is happening and how can I solve this problem?

Answer

Consider these facts:

  • The reference date is Jan 1, 2001.
  • All time interval values before that date are negative.
  • You are using the type uint64_t which can never be negative.

There is a dedicated type NSTimeInterval (aka double).