How to create a specific date in the distant past, the era of BC

I am trying to create a date in the BC era, but not very hard. The following result returns 4713 as a year, not '-4712:

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *components = [NSDateComponents new]; [components setYear: -4712]; NSDate *date = [calendar dateFromComponents:components]; NSLog(@"%d", [[calendar components:NSYearCalendarUnit fromDate: date] year]); 

Any idea what I'm doing wrong?

UPDATE: Work Code

  NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *components = [NSDateComponents new]; [components setYear: -4712]; NSDate *date = [calendar dateFromComponents:components]; NSDateComponents *newComponents = [calendar components:NSEraCalendarUnit|NSYearCalendarUnit fromDate:date]; NSLog(@"Era: %d, year %d", [newComponents era], [newComponents year]); 

This prints 0 for an era, as Ben explained.

+6
cocoa nsdate nsdatecomponents
source share
1 answer

Your code really works fine. Since theres no year zero, -4712 is the year 4713 BC If you check the component of an era, you will see that its zero, which in the Gregorian calendar indicates BC. Flip this negative sign and you will see 4712 AD (era 1).

+6
source share

All Articles