내가 다음 코드서식있는 NSDate 객체 내가 HH "로 날짜를 포맷 할
-(NSDate *)getCurrentDateinLocalTimeZone
{
NSDate* sourceDate = [NSDate date];
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] ;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
return [dateFormatter dateFromString: [dateFormatter stringFromDate:destinationDate]];
}
으로 내 응용 프로그램에서 다른 지점에서 사용자 시간대에 현재있는 NSDate를 얻을 시간대를 변경 : mm를 두 번째 방법의 출력이 1에있어서의 결과를 3 시간 시프트하는 경우 UI 목적은 그래서 다음의 방법을
-(NSString *)formatDate:(NSDate *)date
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
dateFormatter.dateFormat = @"HH:mm";
return [dateFormatter stringFromDate:date];
}
을 사용하기 위해 ",,, 나만있는 NSDate의 포맷을하지 변경할 시간, 내가 뭘 잘못하고있어?
첫 번째 루틴은 날짜의 서식을 지정/인쇄하지 않습니다. 그리고 왜 첫 번째 루틴에서 마지막 줄을 가지고 있습니까? 'destinationDate' 만 리턴하면됩니다. –
출력의 마지막 3 줄을 사용하십시오 – ahmad
첫 번째 루틴의 마지막 줄은 단순히 날짜를 char 형식으로 변환 한 다음 NSDate 형식으로 다시 변환합니다. NSDate 객체에 *** *** 형식 (또는 시간대)이 저장되어 있지 않습니다. –