Ham Dong Kyun
Ham Dong Kyun

Reputation: 776

Setting NSTimeInterval from NSDate

I have searched through the internet and found that [NSTimeInterval] can be set using following code.

myCalendarDay.date = [date timeIntervalSince1970];

What I don't get is why the debugger showing the values as follows. [date] is [NSDate] type while [myCalendarDay.date] is [NSTimeInterval] type. I was checking their values and saw that the year is way off by 31 years.

Is this correct data?

enter image description here

Upvotes: 0

Views: 353

Answers (1)

Jody Hagins
Jody Hagins

Reputation: 28409

timeIntervalSince1970 returns a NSTimeInterval and represents the number of seconds that have elapsed since the first time instant of 1 January 1970, GMT.

When you store an NSDate it actually stores the number of seconds since the first instant of 1 January 2001, GMT.

You are asking the date object to give you a representation with one reference date, then you are assigning that value to another date object (most likely a core data object that has been told to use native values when generating the subclass code). It takes the time interval, and ASSUMES it is using the proper reference date.

There are 31 years between those two dates, and not so coincidentally, there are 31 years between the times in your date object, and the date in the core data instance.

If you truly want to use 1970 as your basis, you should be creating a date with dateWithTimeIntervalSince1970:.

Upvotes: 1

Related Questions