iOS 有关日期操作

//************有关日期的处理************

        NSDateFormatter *inputFormatter = [[[NSDateFormatteralloc]init] autorelease];

        [inputFormatter setLocale:[[[NSLocalealloc]initWithLocaleIdentifier:@"en_US"]autorelease]];

        [inputFormatter setDateFormat:@"MMM dd,yyyy"];// 根据需要设置格式

        NSDate *date = [inputFormatter dateFromString:dateString];;

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

        NSCalendar *calendar = [NSCalendarcurrentCalendar];

        NSUInteger unitFlags =NSYearCalendarUnit |NSMonthCalendarUnit |NSDayCalendarUnit |NSHourCalendarUnit |NSMinuteCalendarUnit |NSSecondCalendarUnit;

        NSDateComponents *dateComponent = [calendarcomponents:unitFlagsfromDate:date];

        int year = [dateComponent year];

        int month = [dateComponent month];

        int day = [dateComponent day];

        NSLog(@"year is: %d", year);

        NSLog(@"month is: %d", month);

        NSLog(@"day is: %d", day);

你可能感兴趣的:(iOS 有关日期操作)