Im trying to convert a String without timezone value like "31.05.2015" to NSDate.
Whats wrong with my DateFormatter? It returns the date 2015-05-31 22:00:00 +0000 and when I try to print it with the same formatter it prints: 01.06.2015
-(NSDateFormatter*) dateFormatter{
if(!_dateFormatter){
_dateFormatter = [[NSDateFormatter alloc]init];
[_dateAndTimeFormatter setDateFormat:@"dd.MM.yyyy"];
[_dateAndTimeFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"CEST"]];
_dateFormatter.dateStyle = NSDateFormatterShortStyle;
_dateFormatter.timeStyle = NSDateFormatterNoStyle;
}
return _dateFormatter;
}
EDIT
I updated it to
-(NSDateFormatter*) dateAndTimeFormatter{
if(!_dateAndTimeFormatter){
_dateAndTimeFormatter = [[NSDateFormatter alloc]init];
[_dateAndTimeFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS Z"];
_dateAndTimeFormatter.dateStyle = NSDateFormatterShortStyle;
_dateAndTimeFormatter.timeStyle = NSDateFormatterShortStyle;
}
return _dateAndTimeFormatter;
}
and call it with
NSDate *startDate = [self.dateFormatter dateFromString:@"31.05.2015"];
self.beginningValueLabel.text = [self.dateFormatter stringFromDate:startDate];
The result still is 01.06.2015.
startDate is 2015-05-31 22:00:00 +0000
NSLog()or the debugger to see the resulting date? A little searching might find the 1000+ other questions relating to this on this site. - Droppy