-2

I have a date timestamp in milliseconds: 145146240000

I create an NSDate object from that timestamp:

NSTimeInterval seconds = [@"1451462400000" doubleValue] / 1000.0f;
NSDate *transactionDate = [NSDate dateWithTimeIntervalSince1970:seconds];
NSLog(@"%@", transactionDate);

I log the value of transactionDate: 2015-12-30 08:00:00 +0000

I then output this date using an NSDateFormatter:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"d MMMM YYYY"];
NSLog(@"%@", [dateFormatter stringFromDate:transactionDate]);

This incorrectly outputs the year to be 2016!:

30 December 2016

How can this be? My local timezone is GMT+2 so if anything it could be off by two hours when parsed by the formatter but not an entire year!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
jwswart
  • 1,226
  • 14
  • 16

1 Answers1

2

Please use yyyy for year format like

[dateFormatter setDateFormat:@"d MMMM yyyy"];

i hope it will help you.

for more info please have a look

Y returns 2012 while y returns 2011 in SimpleDateFormat

Community
  • 1
  • 1
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66