1. 程式人生 > >iOS 獲取當前時間戳和時間

iOS 獲取當前時間戳和時間

        NSDate *senddate = [NSDatedate];

NSLog(@"date1時間戳 = %ld",time(NULL));

        NSString *date2 = [NSStringstringWithFormat:@"%ld", (long)[senddatetimeIntervalSince1970]];

        NSLog(@"date2時間戳 = %@",date2);

        NSDateFormatter *dateformatter = [[NSDateFormatteralloc] init];

        [dateformatter setDateFormat

:@"YYYY-MM-dd HH:mm:ss"];

        NSString *date1 = [dateformatter stringFromDate:senddate];

        NSLog(@"獲取當前時間   = %@",date1);

// 時間戳轉時間

NSDate *confromTimesp = [NSDatedateWithTimeIntervalSince1970:[senddate timeIntervalSince1970]];

        NSString *confromTimespStr = [dateformatterstringFromDate

:confromTimesp];

        NSLog(@"時間戳轉時間   = %@",confromTimespStr);

// 兩個時間戳的間隔   返回的是秒

        NSTimeInterval time = [now timeIntervalSinceDate:oldDate];

        NSLog(@"time:%f",time/60/60/24);