使用NSDateFormatter
修改日期格式,結果日期比給定的日期早一天。
代碼如下:
NSString *strDate = [self.mArrEventDate objectAtIndex:i];
NSString *str = [[NSString alloc] init];
str = [strDate stringByReplacingOccurrencesOfString:@"/" withString:@"-"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// ...using a date format corresponding to your date
[dateFormatter setDateFormat:@"dd/MM/yyyy"];
// Parse the string representation of the date
NSDate *date = [dateFormatter dateFromString:strDate];
NSDateFormatter *format1 = [[NSDateFormatter alloc]init];
[format1 setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[format1 setDateFormat:@"yyyy-MM-dd"];
NSString *sDate = [format1 stringFromDate:date];
[format1 setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
估計是時區設置不正確引起的。