Search code examples
objective-cnsdate

I want exact date when I formatted Objective-C


NSDate *date = 2016-11-07 23:00:00 +0000 2016-11-08
NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];

[formatter1 setFormatterBehavior:NSDateFormatterBehavior10_4];
[formatter1 setDateStyle:NSDateFormatterShortStyle];
[formatter1 setTimeStyle:NSDateFormatterNoStyle];
[formatter1 setDateFormat:@"yyyy-MM-dd"];

NSString *beginningOfWeek1 = [formatter1 stringFromDate: date];
NSLog(@"beginigofweek is %@"beginningOfWeek1);

The output I get is

2016-11-08

but I want only exact date.


Solution

  • NSDate uses UTC and displays dates in UTC. Date formatter assumes your local time zone. If the NSDate shows 23:00:00 then it is 4:30:00 on the next day in the India.