0

Suppose, my birthdate is (mon/day/year) 10-05-1932 (as NSDate), how to convert this to NSTimeInterval.

Again, how to convert back that NSTimeInterval to NSDate back?

I tried using different methods of NSDate but haven't succeed yet.

What I'm doing?

NSString *strdate = @"10-05-1932";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"MM-dd-yyyy"];
NSDate *date = [df dateFromString:strdate];
NSLog(@"%.f", -[date timeIntervalSinceNow]);

This logs 2616605071 (as on 4th September 2015 at 16:21) – When I checked it with the site like this it gives me wrong date.

Hemang
  • 26,840
  • 19
  • 119
  • 186

2 Answers2

2
NSTimeInterval timeInterval = date.timeIntervalSinceReferenceDate;
NSDate *anotherDate = [NSDate dateWithTimeIntervalSinceReferenceDate: timeInterval];

Try the code below. date and anotherDate will be identical.

NSCalendar *calendar = [NSCalendar currentCalendar];
calendar.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:10];
[components setMonth:5];
[components setYear:1934];
NSDate *date = [calendar dateFromComponents:components];
NSLog(@"%@", date);
NSTimeInterval timeInterval = [date timeIntervalSinceReferenceDate];
NSDate *anotherDate = [NSDate dateWithTimeIntervalSinceReferenceDate:timeInterval];
NSLog(@"%@", anotherDate);

UPDATE:

It's incorrect because you get timestamp (time interval) from that website which use UNIX timestamp. Also, it's incorrect because you use timeIntervalSinceNow which will likely change every time you call the method because it's relative to the current time. If you want the date/time interval that compatible with that website. Use:

NSTimeInterval timeInterval = date.timeIntervalSince1970;
NSDate *anotherDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];

You can copy the timeInterval from the code above (-1188000000) and paste it on the website and it will give you a correct date.

Internally, NSDate store time interval relative to reference date (Jan 1st, 2001). The website you mentioned is UNIX timestamp that relative to Jan 1st, 1970.

yusuke024
  • 2,189
  • 19
  • 12
  • This is also giving me wrong results. Please check edited question. I think, `timeIntervalSinceReferenceDate` would not help for earlier dates. – Hemang Sep 04 '15 at 12:54
  • The `timeInterval` will be negative if it's before the reference date (Jan 1st, 2001). At least this is what happens on my machine. – yusuke024 Sep 04 '15 at 13:13
  • Thanks a lot, one question, the second way is proper or not? `NSTimeInterval timeInterval = date.timeIntervalSince1970;` ? – Hemang Sep 04 '15 at 13:37
  • If you don't care about the value, (e.g. you don't have to send it to some servers), `timeIntervalSinceReferenceDate` is a way to go, because it's what `NSDate` stores internally. Using `timeIntervalSince1970` will make `NSDate` to convert it to reference date before storing. But it should be no problems at all to use 1970 version if you're consistent and always use the same pair of methods to convert back and forth. – yusuke024 Sep 04 '15 at 13:42
0

This is just worked!

NSString *strdate = @"10-05-1932";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"MM-dd-yyyy"];
NSDate *date = [df dateFromString:strdate];
NSLog(@"%@",date);
NSTimeInterval interval = [date timeIntervalSince1970];
NSLog(@"%f", interval);
NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:interval];
NSLog(@"%@",date2);

Thanks to @sikhapol

Hemang
  • 26,840
  • 19
  • 119
  • 186