2012-09-05 6 views
0

아래의 코드 조각을 시도해 보았습니다. 선택된 시간대 코드 대신 현재 시스템의 날짜와 시간이 표시됩니다.선택한 시간대의 시간과 날짜를 확인 하시겠습니까?

NSDate *now = [NSDate date]; 
NSCalendar *gregorian=[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
[gregorian setTimeZone:[NSTimeZone timeZoneWithName:selectedzone]]; 

NSDateComponents* timeZoneComps = [gregorian components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSTimeZoneCalendarUnit fromDate:now]; 

NSDate *selectedDate=[gregorian dateFromComponents:timeZoneComps]; 
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
[formatter setDateStyle:NSDateFormatterFullStyle]; 
[formatter setDateFormat:@"dd-MM-yyyy HH:mm:ss "]; 
NSString *strSelectedDate= [formatter stringFromDate:selectedDate]; 
setZone.text = strSelectedDate;` 

아이디어가 있으십니까?

답변

1

[NSDate date]는 사용자가 현재 위치에 상관없이 현재 날짜와 시간을 나타내는 날짜 객체를 반환합니다. NSDates는 장소 나 시간대의 영향을받지 않습니다. 그 문제에 대한 현재 또는 다른 순간을 나타내는 하나의 NSDate 만 존재하며 시간대마다 다른 날짜 객체는 아닙니다. 따라서 시간대간에 날짜를 변환하지 마십시오.

NSDate objects represent an absolute instant in time. Consider the following example of how two date representations in different time zones (9/9/11 3:54 PM in Paris and 9/9/11 11:54 PM in Sydney) are actually the same date. 
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
    [formatter setDateStyle:NSDateFormatterShortStyle]; 
    [formatter setTimeStyle:NSDateFormatterShortStyle]; 
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"Europe/Paris"]]; 
    NSDate *aDate = [formatter dateFromString:@"9/9/11 3:54 PM"]; 
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"Australia/Sydney"]]; 
    NSDate *anotherDate = [formatter dateFromString:@"9/9/11 11:54 PM"]; 
    NSLog(@"%@",anotherDate); 
    if ([aDate isEqualToDate:anotherDate]) { 
     NSLog(@"How about that?"); 
    } 

는 출력에있는 NSDate의 설명 방법은 GMT 시간을 반환 염두에두고 날짜, 곰을 제공하고 등 파리, 시드니에서 현지 시간에서을 나타내는 날짜 문자열을 만들기 위해 NSDateFormatter를 사용해야하는 경우 날짜 :

NSDate *now = [NSDate date]; 
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
[formatter setDateStyle:NSDateFormatterShortStyle]; 
[formatter setTimeStyle:NSDateFormatterShortStyle]; 
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"Australia/Sydney"]]; 
NSLog(@"%@",[formatter stringFromDate:now]); //--> 9/9/11 11:54 PM 
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"Europe/Paris"]]; 
NSLog(@"%@",[formatter stringFromDate:now]); //--> 9/9/11 3:54 PM 
+0

이게 당신이 원했던가요? – prashant

+0

고마워요. 많은 감사합니다. –

0
[dateFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss z"]; 

표준 시간대를 표시하지 않으려면 형식 문자열에서 후행 'z'문자를 제거하십시오.

한편 EDIT

, 당신은 단지 그냥 'Z'대문자을하기 위해선, 시간대 이름을 표시하려면.

편집

소문자 'z는'다른 모든 시간대에 잘 작동하지만 불행하게도 GMT는 특별한 경우이다. 따라서 가장 쉬운 방법은 'z'를 생략하고 서식있는 날짜에 'GMT'를 추가하는 것입니다.

+0

는 I가 변화하지만, 현재 시간과 시스템의 날짜를 표시하는 시간대의 시간 또는 날짜 –

+0

NSDateFormatter * 포맷터 = [NSDateFormatter의 ALLOC] INIT]을 선택하지 않았다; [formatter setDateStyle : NSDateFormatterShortStyle]; [formatter setTimeStyle : NSDateFormatterShortStyle]; [formatter setTimeZone : [NSTimeZone timeZoneWithName : @ "유럽/파리"]]; NSDate * aDate = [포맷터 dateFromString : @ "9/9/11 3:54 PM"]; [formatter setTimeZone : [NSTimeZone timeZoneWithName : @ "호주/시드니"]]; NSDate * anotherDate = [포맷터 dateFromString : @ "9/9/11 11:54 PM"]; NSLog (@ "% @", anotherDate); if ([aDate isEqualToDate : anotherDate]) { NSLog (@ "어때?"); } – prashant

관련 문제