투케이2K

31. (Objective-C/objc) 타임 스탬프 (밀리 세컨드) , Date 날짜 형 변환 수행 실시 - NSDate , NSTimeInterval 본문

Objective-C

31. (Objective-C/objc) 타임 스탬프 (밀리 세컨드) , Date 날짜 형 변환 수행 실시 - NSDate , NSTimeInterval

투케이2K 2022. 3. 7. 09:59

[개발 환경 설정]

개발 툴 : XCODE

개발 언어 : OBJECTIVE-C

 

[testMain 함수]

// MARK: - [헤더 파일에 정의 없이 : void 메소드 구현]
- (void)testMain {
    printf("\n");
    printf("=============================== \n");
    printf("[ViewController >> testMain() :: 테스트 메소드 수행] \n");
    printf("=============================== \n");
    printf("\n");
    
    
    // [현재 날짜 및 시간 >> 타임 스탬프 변환 : 밀리 세컨드]
    NSDate *stampDate = [NSDate date];
    NSTimeInterval stampInterval = [stampDate timeIntervalSince1970];
    long long stampMilliseconds = stampInterval * 1000;
    
    printf("\n");
    printf("=============================== \n");
    printf("[타임 스탬프 :: %lld] \n", stampMilliseconds);
    printf("[길이 :: %lu] \n", [NSString stringWithFormat:@"%lld", stampMilliseconds].length);
    printf("=============================== \n");
    printf("\n");
    
    
    
    // [타임 스탬프 변환 >> 현재 날짜 및 시간 변환]
    NSTimeInterval milliseconds = stampMilliseconds; // 타임 스탬프 [밀리 세컨드]
    NSTimeInterval seconds = milliseconds / 1000.0;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:seconds];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; // date 형식 포맷
    [formatter setDateFormat:@"yyyy-MM-dd kk:mm:ss"];
    NSString *nowDate = [formatter stringFromDate:date];
    
    printf("\n");
    printf("=============================== \n");
    printf("[현재 날짜 및 시간 :: %s] \n", nowDate.description.UTF8String);
    printf("=============================== \n");
    printf("\n");

}
 

[결과 출력]


반응형
Comments