투케이2K

132. (Objective-C/objc) string 형태 두날짜 차이 Date 변환 및 두날짜 빼기 수행 시간 (HH), 분 (mm), 초 (ss) 차이 확인 실시 본문

Objective-C

132. (Objective-C/objc) string 형태 두날짜 차이 Date 변환 및 두날짜 빼기 수행 시간 (HH), 분 (mm), 초 (ss) 차이 확인 실시

투케이2K 2022. 11. 1. 17:25
반응형

[개발 환경 설정]

개발 툴 : XCODE

개발 언어 : OBJECTIVE-C

 

[소스 코드]

// MARK: - [헤더 파일에 정의 없이 : void 메소드 구현]
- (void)testMain {
    printf("\n");
    printf("==================================== \n");
    printf("[ViewController >> testMain() :: 테스트 메소드 수행] \n");
    printf("==================================== \n");
    printf("\n");

    
    // [try catch 구문 정의 실시]
    @try {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            // [날짜 변수 선언 실시]
            NSString *oneString =  @"2022-11-02 16:30:40";
            NSString *twoString =  @"2022-11-01 15:25:40";
            
            
            // [string to timestamp 변환 실시]
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            
            NSDate *oneDate = [dateFormatter dateFromString:oneString];
            NSDate *twoDate = [dateFormatter dateFromString:twoString];
            
            NSTimeInterval oneStampInterval = [oneDate timeIntervalSince1970];
            NSTimeInterval twoStampInterval = [twoDate timeIntervalSince1970];
            
            long long oneMilliseconds = oneStampInterval * 1000;
            long long twoMilliseconds = twoStampInterval * 1000;
            
            
            // [두 날짜 차이 확인 실시]
            long long minusMilliseconds = oneMilliseconds - twoMilliseconds; // [밀리세컨드 빼기]
            
            long long minusSecond = minusMilliseconds / 1000; // [초 차이]
            long long minusMin = minusSecond / 60; // [분 차이]
            long long minusHour = minusMin / 60; // [시간 차이]

            
            // [로그 출력 실시]
            NSLog(@"==================================== \n");
            NSLog(@"[ViewController >> testMain() :: NSLog 로그 결과 확인 실시] \n");
            NSLog(@"==================================== \n");
            NSLog(@"[oneString :: %@] \n", oneString);
            NSLog(@"==================================== \n");
            NSLog(@"[twoString :: %@] \n", twoString);
            NSLog(@"==================================== \n");
            NSLog(@"[oneMilliseconds :: %lld] \n", oneMilliseconds);
            NSLog(@"==================================== \n");
            NSLog(@"[twoMilliseconds :: %lld] \n", twoMilliseconds);
            NSLog(@"==================================== \n");
            NSLog(@"[minusMilliseconds :: %lld] \n", minusMilliseconds);
            NSLog(@"==================================== \n");
            NSLog(@"[minusSecond :: %lld] \n", minusSecond);
            NSLog(@"==================================== \n");
            NSLog(@"[minusMin :: %lld] \n", minusMin);
            NSLog(@"==================================== \n");
            NSLog(@"[minusHour :: %lld] \n", minusHour);
            NSLog(@"==================================== \n");

        });

    }
    @catch (NSException *exception) {
        printf("\n");
        printf("==================================== \n");
        printf("[ViewController >> catch :: 예외 상황 확인] \n");
        printf("[name :: %s] \n", exception.name.description.UTF8String);
        printf("[reason :: %s] \n", exception.reason.description.UTF8String);
        printf("==================================== \n");
        printf("\n");
    }
}
 

[결과 출력]


반응형
Comments