0

请帮我解决问题。

(objValidation.aRange.MinimumValue、objValidation.aRange.MaximumValue 的值来自网络服务)

(我在 firstDate、secondDate 的日期有误。)

NSDateFormatter *df  = [[NSDateFormatter alloc] init];
                [df setDateFormat:@"MM/dd/yyyy"];
                [df setTimeZone:[NSTimeZone localTimeZone]];
                NSDate *myDate = [df dateFromString:inputString];
                [df setDateFormat:@"MM/dd/yyyy"];
                NSLog(@"objValidation.aRange.MinimumValue : %@,objValidation.aRange.MaximumValue :%@, inputString : %@",objValidation.aRange.MinimumValue,objValidation.aRange.MaximumValue,inputString);
                NSDate *firstDate = [df dateFromString:objValidation.aRange.MinimumValue];
                NSDate *secondDate = [df dateFromString:objValidation.aRange.MaximumValue];
                NSLog(@"myDate : %@,firstDate :%@, secondDate : %@",myDate,firstDate,secondDate);
                [df release];

输出:

objValidation.aRange.MinimumValue : 8/20/2013
objValidation.aRange.MaximumValue :12/31/9999
inputString : 08/22/2013 
myDate : 2013-08-21 18:30:00 +0000
firstDate :2013-08-19 18:30:00 +0000
secondDate : 1999-12-30 18:30:00 +0000 
4

2 回答 2

2

重新调整的日期是正确的,但您没有考虑时区。

所有NSDate对象都没有时区。因此,当您解析日期时,将使用系统时区。

于 2013-08-20T11:46:47.847 回答
-3

试试这个代码:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"MM/dd/yyyy"];
NSDate *date = [df dateFromString:@"8/20/2013"];
NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone];
NSTimeZone *utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:date];
NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:date];
NSTimeInterval gmtInterval = currentGMTOffset - gmtOffset;
NSDate *destinationDate = [[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:date];
NSLog(@"DateString : %@", destinationDate);

您将获得所需的准确输出。

于 2013-08-20T12:28:46.360 回答