Ios 目标C中UI中显示日期(显示1天以下)时的时区问题

Ios 目标C中UI中显示日期(显示1天以下)时的时区问题,ios,objective-c,xcode,nsdate,nsdateformatter,Ios,Objective C,Xcode,Nsdate,Nsdateformatter,我面临一个问题,与不同的时区有关 在UI中,我将日期设置为2018年3月11日 leaveEndDate->3/11/18 在服务中我得到了结果 leaveEndDate=2018-03-11T06:00:00Z 但当我在UI中显示日期时,它显示的是2018年3月10日,但实际结果应该是2018年3月11日 它在应用程序中显示的时间少于1天 我已经改变了时区->华盛顿特区-美国,它应该适用于所有时区 接近 //调整日期 NSDate* originalDateValue = [s

我面临一个问题,与不同的时区有关

在UI中,我将日期设置为2018年3月11日

leaveEndDate->3/11/18

在服务中我得到了结果

leaveEndDate=2018-03-11T06:00:00Z

但当我在UI中显示日期时,它显示的是2018年3月10日,但实际结果应该是2018年3月11日

它在应用程序中显示的时间少于1天

我已经改变了时区->华盛顿特区-美国,它应该适用于所有时区

接近

//调整日期

        NSDate* originalDateValue = [self valueForKey:actualKey];

        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss Z"; // tried formatter-> @"yyyy-MM-dd'T'HH:mm:ss'Z'"; // yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ
        dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
        NSString *dateString = [dateFormatter stringFromDate:originalDateValue];

        dateFormatter.timeZone = [NSTimeZone localTimeZone];

        NSDate * actualDate = [dateFormatter dateFromString:dateString];

        [self setValue:actualDate forKey:actualKey];
NSDate* originalDateValue = [self valueForKey:actualKey];
NSDate* adjustedDateValue = [self.class adjustDate:originalDateValue forTimezone:[self timeZoneName]];

    +(NSDate*)adjustDate:(NSDate*)originalDateValue forTimezone:(NSString*)timezoneName
    {

    if([originalDateValue isEqual:[NSNull null]]) return nil;

    NSTimeInterval timeInterval = ([[NSTimeZone timeZoneWithName:timezoneName] secondsFromGMTForDate:originalDateValue] - [[NSTimeZone localTimeZone] secondsFromGMTForDate:originalDateValue]);

    NSDate* adjustedDateValue = [originalDateValue initWithTimeInterval:timeInterval sinceDate:originalDateValue]; //[originalDateValue dateByAddingTimeInterval:timeInterval];

    return adjustedDateValue;
    }
我使用的另一种方法是:

//调整日期

        NSDate* originalDateValue = [self valueForKey:actualKey];

        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss Z"; // tried formatter-> @"yyyy-MM-dd'T'HH:mm:ss'Z'"; // yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ
        dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
        NSString *dateString = [dateFormatter stringFromDate:originalDateValue];

        dateFormatter.timeZone = [NSTimeZone localTimeZone];

        NSDate * actualDate = [dateFormatter dateFromString:dateString];

        [self setValue:actualDate forKey:actualKey];
NSDate* originalDateValue = [self valueForKey:actualKey];
NSDate* adjustedDateValue = [self.class adjustDate:originalDateValue forTimezone:[self timeZoneName]];

    +(NSDate*)adjustDate:(NSDate*)originalDateValue forTimezone:(NSString*)timezoneName
    {

    if([originalDateValue isEqual:[NSNull null]]) return nil;

    NSTimeInterval timeInterval = ([[NSTimeZone timeZoneWithName:timezoneName] secondsFromGMTForDate:originalDateValue] - [[NSTimeZone localTimeZone] secondsFromGMTForDate:originalDateValue]);

    NSDate* adjustedDateValue = [originalDateValue initWithTimeInterval:timeInterval sinceDate:originalDateValue]; //[originalDateValue dateByAddingTimeInterval:timeInterval];

    return adjustedDateValue;
    }
第三种方法

NSDate* originalDateValue = [self valueForKey:actualKey];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSSZ";
[dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]];
NSString *dateString = [dateFormatter stringFromDate:originalDateValue];
NSDate* adjustedDateValue = [dateFormatter dateFromString:dateString];
有什么帮助吗?

originalDateValue始终设置为UTC时间,表示您给出的日期。NSDate在UTC时间工作,因为您没有指定时区。dateFormatter正在将您提供的输入转换为您的localtime,这比您的localtime晚了几个小时。因为您没有指定时间,所以它会将时间设置为午夜00:00:00,所以在从UTC转换到华盛顿特区时,您会将时区延迟数小时

您必须根据UTC到本地时区的偏移量调整源日期:


然后按照您提供的方式操作destinationDate。

请显示相关代码;您如何创建NSDate以及如何在UI中显示日期。此处的代码与您在问题中的陈述不匹配。您声明以一种格式的日期字符串开始,并希望将其转换为本地时区中的新字符串。但您的代码并没有试图完成所有这些任务。@rmaddy如何解决此问题?您首先需要更新您的问题,并澄清您实际拥有的内容和实际需要的内容。不清楚,因为您当前的代码与您当前的描述不匹配。@rmaddy我已更新我的问题。请单击[initWithTimeInterval:interval sinceDate:originalDateValue]->什么是initWithTimeInterval?返回一个相对于另一个给定日期初始化了给定秒数的日期对象。很抱歉,这个答案中的所有内容都是错误的。期待看到你的答案。