- (NSDate *)dateWithStr:(NSString *)timeStr{
NSDateFormatter *formatter = [NSDateFormatter new];
formatter.dateFormat = @"YYYY-MM-dd HH:mm:ss";
NSDate *date = [formatter dateFromString:timeStr];
// 将时间转为当前时区
NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
NSInteger interval = [timeZone secondsFromGMTForDate:date];
NSDate *localeDate = [date dateByAddingTimeInterval:interval];
return localeDate;
}
- (NSString *)stringWithDate:(NSDate *)date{
NSDateFormatter *formatter = [NSDateFormatter new];
formatter.dateFormat = @"YYYY-MM-dd HH:mm:ss";
// 将时间转为当前时区
NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
NSInteger interval = [timeZone secondsFromGMTForDate:date];
NSDate *localeDate = [date dateByAddingTimeInterval:-interval];
NSString *str = [formatter stringFromDate:localeDate];
return str;
}
网友评论