NSDate日常使用方法总结

(注:持续更新)
1、时间转时间戳:

- (void)intervalTimeWithDate {
    //当前时间
    NSDate *date = [NSDate date];
    
    NSTimeInterval timeInterval = [date timeIntervalSince1970]*1000;
    
    NSLog(@"currentDate timeInterval = %f",timeInterval);
}

2、时间戳转时间:

- (void)dateWithIntervalTime {
    //当前时间
    NSTimeInterval timeInterval = 1474283450000;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval/1000];
    
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *dateStr = [formatter stringFromDate:date];
    NSLog(@"date = %@",dateStr);
}

3、倒计时

- (void)timeIntervalCompare {
    
    NSTimeInterval timerInterval1 = 1474283450000;
    NSTimeInterval timerInterval2 = 1473483450000;
    
    NSInteger timeInterval = (timerInterval2 - timerInterval1)/1000;
    
    int day = (int)(timeInterval/86400);
    int hour = (int)((timeInterval - day*86400)/3600);
    int minute = (int)(timeInterval - day*86400 - hour*3600)/60;
    int second = (int)(timeInterval - day*86400 - hour*3600 - minute*60);
    
    NSLog(@"%d天%d时%d分%d秒",day,hour,minute,second);
    
}

你可能感兴趣的:(NSDate日常使用方法总结)