2013-02-22 5 views
3

NSDateFormatter를 사용하여 산정 표준 시간으로 저장된 날짜를 변경하려고하지만 작동하지 않는 것 같습니다.NSDateFormatter stringFromDate 문자열을 반환하지 않습니다.

이 "하는 currentDate : 날짜 포맷으로 2013년 2월 22일 23시 20분 20초 0000 날짜 : 2000-01-01 7시 0분 0초 0000 기타"출력 년대 콘솔에 합니다.

문자열이 제대로 작성되지 않은 것처럼 보입니다. 그러나 정상적으로 호출 된 것과 같은 방식으로 호출하는 것 같습니다.

제안 사항?

NSTimeZone * mtnTimeZ= [NSTimeZone timeZoneWithAbbreviation:@"MST"]; 

    NSDate *currentDate = [NSDate date]; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; 
    [dateFormatter setTimeZone:mtnTimeZ]; 

    NSString * timeZ = [dateFormatter stringFromDate:currentDate]; 

    NSDate * newDate = [dateFormatter dateFromString:timeZ]; 
    NSLog(@"currentDate: %@ string With dateFormatter: %@ date made from string %@", currentDate, timeZ, newDate); 
+1

NSLog의 출력을 표시하지 않습니다. 텍스트가 다릅니다. CURRENT 코드 (붙여 넣기를 했습니까?)와 그 코드의 최신 결과를 붙여 넣었는지 다시 한 번 확인하십시오. –

+0

저장된 날짜의 형식은 무엇입니까? 게시 한 코드는 약간 인위적입니다. NSDate에서 NSString으로 이동 한 다음 NSDate로 돌아가서 특정 포맷 세트가없는 포맷터를 사용합니다. 당신이 정말로 구원 한 것과 그것이 무엇으로 변환되어야하는지 말해주십시오. – rmaddy

답변

4

당신은 일에 순서대로 날짜 및 시간 형식을 지정의 스타일을 설정해야합니다

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateStyle:NSDateFormatterFullStyle]; 
[dateFormatter setTimeStyle:NSDateFormatterFullStyle]; 

[dateFormatter setTimeZone:[NSTimeZone defaultTimeZone]]; 
NSString * dateCurrentTZ = [dateFormatter stringFromDate:[NSDate date]]; 

[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"MST"]]; 
NSString * dateMSTZ = [dateFormatter stringFromDate:[NSDate date]]; 

NSLog(@"Date In Current Time Zone: %@", dateCurrentTZ); 
NSLog(@"Date In MST: %@", dateMSTZ); 

자신의 형식을 지정하려면 :

NSDateFormatter Documentation

다음 버전 아이폰 OS & 맥 OSX에 따라 "고정 형식"을 찾습니다. 당신은 표적으로하고있다.

+0

사과의 문서에서 포맷터가 주어진 날짜의 형식을 따르는 것처럼 보였으 나 외관상으로는 보이지 않았다. 정말로 사과가 iOS가 OSX의 dateWithCalendarFormat : timeZone 메소드를 NSDate에 사용할 수있게 해주면 좋을 것입니다. – PeejWeej

1

봅니다 다음 줄을 넣어 :이 같은

[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"]; 

:

NSTimeZone * mtnTimeZ= [NSTimeZone timeZoneWithAbbreviation:@"MST"]; 

NSDate *currentDate = [NSDate date]; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; 
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"]; 
[dateFormatter setTimeZone:mtnTimeZ]; 

NSString * timeZ = [dateFormatter stringFromDate:currentDate]; 

NSDate * newDate = [dateFormatter dateFromString:timeZ]; 
NSLog(@"currentDate: %@ string With dateFormatter: %@ date made from string %@", currentDate, timeZ, newDate); 
+0

날짜 형식은 기본적으로 로켈의 형식이어야합니다. –

+0

@HotLicks 형식을 지정해야합니다. 문서에 기본 날짜 및 시간대 스타일이 설정되지 않은 경우 Apple에서 고정 형식을 사용하거나 정의하도록 지정해야합니다. – SAPLogix

+0

currentDate : 2013-02-23 22:04:17 +0000 string with dateFormatter : 2013-02-23 03:04:17 date 만든 문자열 2013-02-23 10:04:17 +0000 코드에서 , 거기에 무슨 일이 일어나고 있는지 모르겠다. – PeejWeej

관련 문제