Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSDateFormatter - Adding a Year? [duplicate]

I have the following code which is simply getting the current date and converting it to the format YYYY-MM-dd HH:mm:ss zzz

        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat: @"YYYY-MM-dd HH:mm:ss zzz"];
        NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone];
        [dateFormatter setTimeZone:currentTimeZone];

        NSString *time = [dateFormatter stringFromDate:[NSDate date]];

        NSLog(@"TIME IS %@", time);

With this, I'm getting the following log output:

2014-12-30 00:20:38.987 MYAPPNAME[953:229825] TIME IS 2015-12-30 00:20:38 CST

Can someone tell me why the output is showing as 2015, rather than 2014 as it should be?

like image 440
AdamTheRaysFan Avatar asked Apr 26 '26 09:04

AdamTheRaysFan


2 Answers

yyyy specifies the calendar year, whereas YYYY specifies the year (of “Week of Year”)

you must change your dateformatter style from

[dateFormatter setDateFormat: @"YYYY-MM-dd HH:mm:ss zzz"];

to

[dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss zzz"];

for your reference, check this apple documentation

like image 190
arthankamal Avatar answered Apr 28 '26 00:04

arthankamal


change your date formate

[dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss zzz"];
like image 39
karthikeyan Avatar answered Apr 27 '26 23:04

karthikeyan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!