时间日期比较

来源:互联网 发布:ip地址和域名 编辑:程序博客网 时间:2024/05/17 23:17
获取当前时间(美国时间) NSDate *dateToDay = [NSDate date];    NSDateFormatter *df = [[NSDateFormatter alloc] init];    [df setDateFormat:@"yyyy-MM-DD HH:mm:ss"];    NSLocale *local = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];    [df setLocale:local];    NSString *myDataString = @"2009-09-15 18:30:00";    字符串生成日期的代码    NSDate *myData = [df dateFromString:myDataString];    日期比较    switch ([dateToDay compare:myData]) {        case NSOrderedSame:            NSLog(@"These dates are the same!");            break;                    case NSOrderedAscending:            NSLog(@"dateToDay is earlier than myDate!");            break;                    case NSOrderedDescending:            NSLog(@"mydate is earlier than dateToDay");            break;                    default:            NSLog(@"Bad times. Invalid enum value returned.");            break;    }

0 0
原创粉丝点击