NSDate

获取时间戳

//获取当前时间
NSDate *currentDate = [NSDate date];//获取当前时间,日期
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:currentDate];
NSLog(@"现在时间--->> %@",dateString);

//日期加 60秒以后的时间,时间按照秒计算
NSTimeInterval interval =  60 * 2;
NSString *titleString = [dateFormatter stringFromDate:[currentDate initWithTimeInterval:interval sinceDate:currentDate]];
NSLog(@"未来时间 %@",titleString);

两个时间差 dateFormat为另外一个时间 和现在时间差

+(NSDateComponents *)getDateComponents:(NSString *)date dateFormat:(NSString *)dateFormat
{

    //获取当前时间
    NSDate *nowDate = [NSDate date];

   //创建日期格式化
    NSDateFormatter *dateFomatter = [[NSDateFormatter alloc] init];
    dateFomatter.dateFormat = dateFormat;

   //获取剩余截止时间数据
   // 截止时间字符串格式
   NSString *expireDateStr = date;

   // 当前时间字符串格式
   NSString *nowDateStr = [dateFomatter stringFromDate:nowDate];

  // 截止时间data格式
  NSDate *expireDate = [dateFomatter dateFromString:expireDateStr];

  // 当前时间data格式
  nowDate = [dateFomatter dateFromString:nowDateStr];

// 当前日历
  NSCalendar *calendar = [NSCalendar currentCalendar];

 // 需要对比的时间数据
  NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth
| NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;

// 对比时间差
NSDateComponents *dateCom = [calendar components:unit fromDate: expireDate toDate:nowDate options:0];
  return dateCom;
}

你可能感兴趣的:(NSDate)