NSDate

//初始化格式化时间 并设置时区 时间和NSString之间的转化

NSDateFormatter *formater = [[NSDateFormatter alloc]init];

formater.dateFormat = @"yyyy-MM-dd HH:mm:ss";

[formater setTimeZone:[NSTimeZone timeZoneWithName:@"UTC+8"]];

NSDate * date = [NSDate date];

NSString *date1 = [formater stringFromDate:date];

//增加时间

NSTimeInterval secondes = 24 * 60 * 60;

NSDate * now = [NSDate date];

//模拟昨天 24 * 60 * 60

NSDate * yesDay = [now addTimeInterval:-secondes];

//比较两个日期是否相同

BOOL isEqual = [now isEqualToDate:yesDay];

//获得两个日期中比较早的一个

NSDate * earler = [yesDay earlierDate:now];

NSLog(@"%@",earler);

NSDate * later = [yesDay laterDate:now];

NSLog(@"later %@",later);

//把字符串转换成日期

NSString * str = @"2014/03/20";

NSDateFormatter * f = [[NSDateFormatter alloc] init];

[f setDateFormat:@"yyyy/MM/dd"];

NSDate * date = [f dateFromString:str];

NSLog(@"date %@",date);

你可能感兴趣的:(NSDate)