iOS 日期操作


NSDate * newDate = [NSDate date];

NSDateFormatter * f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSString * str = [f stringFromDate:newDate];
//设置时差计算方法 //GTM
[f setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];

NSLog(@"%@",str);


NSTimeInterval secondes = 24 * 60 * 60;
NSDate * now = [NSDate date];

//模拟昨天 24 * 60 * 60
NSDate * yesDay = [now addTimeInterval:-secondes];
//比较两个日期是否相同
BOOL isEqual = [now isEqualToDate:yesDay];
if (isEqual)
{
    NSLog(@"相同");
}
else
{
    NSLog(@"不相同");
}

//获得两个日期中比较早的一个
NSDate * earler = [yesDay earlierDate:now];
NSLog(@"%@",earler);

NSDate * later = [yesDay laterDate:now];
NSLog(@"later %@",later);




你可能感兴趣的:(ios,NSDate)