码迷,mamicode.com
首页 > 其他好文 > 详细

如何判断两个时间是否是同一天

时间:2015-03-06 11:20:16      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:ios   nsdate   


- (void)viewDidLoad {

    [super viewDidLoad];

    

    NSString *d = @"2015-03-05 23:59:59";

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

    [format setDateFormat:@"yyyy:MM:dd HH:mm:ss"];

    NSDate *date = [format dateFromString:d];

    NSLog(@"%i", [ViewController isCurrentDay:date]);

}



// 判断两个时间是否是同一天

+ (BOOL)isCurrentDay:(NSDate *)aDate

{

    if (aDate==nil) return NO;

    

    NSCalendar *cal = [NSCalendar currentCalendar];

    NSDateComponents *components = [cal components:(NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:[NSDate date]];

    NSDate *today = [cal dateFromComponents:components];

    components = [cal components:(NSCalendarUnitEra|NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:aDate];

    NSDate *otherDate = [cal dateFromComponents:components];

    if([today isEqualToDate:otherDate])

        return YES;

    

    return NO;

}

如何判断两个时间是否是同一天

标签:ios   nsdate   

原文地址:http://blog.csdn.net/qq_21649645/article/details/44096607

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!