티스토리 뷰

코드 예제

NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"dd/MM/yyyy HH:mm"];

NSString *localDateString = [dateFormatter stringFromDate:[NSDate date]];
NSLog(@"date = %@", localDateString); //현재 시간 출력

NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];
NSLog(@"Name: %@", localTimeZone.name); //타임존 출력

NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT:localTimeZone.secondsFromGMT];
NSLog(@"1. GMT: %@", timeZone.abbreviation); //gmt offset
NSLog(@"2. GMT: %@", timeZone.description);
NSLog(@"3. GMT: %@", timeZone.name);
NSLog(@"GMT offset(sec): %ld", (long)[NSTimeZone localTimeZone].secondsFromGMT);

//gmt offset 분단위로 환산
NSInteger offsetInMillis = [NSTimeZone localTimeZone].secondsFromGMT;
NSString *offset = [NSString stringWithFormat:@"%d",abs((int)offsetInMillis/60)];
NSLog(@"GMT offset(min): %@", offset);

NSString *strTimeZone = [NSString stringWithFormat:@"Local;%@;(%@) Local Time;%@;",
                         offset, timeZone.name, localTimeZone.name];

로그 출력

date = 14/08/2020 14:43
Name: Asia/Seoul
1. GMT: GMT+9
2. GMT: GMT+0900 (GMT+9) offset 32400
3. GMT: GMT+0900
GMT offset(sec): 32400
GMT offset(min): 540
댓글