2012-12-07 3 views
1

날짜를 NSString으로 인쇄하는 데 문제가 있습니다. 나는 내 응용 프로그램 수명주기의 특정 지점에서 이런 식으로 현재 날짜와 시간을 저장 한 :NSDate 형식에 예상 내용이 표시되지 않습니다.

NSDate *current = [NSDate date]; 
long currentTime = [current timeIntervalSince1970]; 

지금, 나는 UILabels에 그 날짜를 표시해야합니다. 나는이 방법을 시도했다 :

NSDate *date = [NSDate dateWithTimeIntervalSince1970:currentTime]; 
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
[formatter setDateFormat:@"dd/MM/yyyy - HH:mm:ss"]; 
NSString *stringFromDate = [formatter stringFromDate:date]; 

그러나 나는 정확한 날짜도없고 시간도 얻지 못하고있다. 나는 무엇을 놓칠 수 있 었는가?

감사합니다.

+0

잘못된 것은 무엇입니까? 왜'current' 변수를 사용하고 포맷하지 않습니까? 시간 간격 자료를 사용해야하는 이유는 무엇입니까? 'date'를 로그한다면, 맞습니까? – rmaddy

+0

사소한 포인트 : 시간 간격은'long'이 아니라'double's이기 때문에, 당신은 거기에서 약간의 정밀도를 잃어 버릴 것입니다. (하지만이 컨텍스트에서는 아무런 효과가 없어야합니다). – warrenm

+0

'currentDate'가 정의되지 않았기 때문에'[currentDate timeIntervalSince1970]'은'[current timeIntervalSince1970]'이어야한다고 생각했습니다. – dasblinkenlight

답변

2

이 시도

는 NSString stringFromDate * = [NSDateFormatter localizedStringFromDate : DateStyle의 기간 : NSDateFormatterMediumStyle timeStyle : NSDateFormatterMediumStyle];

또한 데이터 스타일과 팀 스타일을 변경할 수 있습니다.

0

currentTime 변수는 long이며 NSTimeInterval (double)이 아닙니다. 당신은 분수 부분 (덜 중요한)뿐만 아니라 시간 간격의 상한 범위를 잃고 있습니다. 다음으로 변경하십시오 :

NSTimeInterval currentTime = [current timeIntervalSince1970]; 
관련 문제