nsdateutil

-(void)dateUtil

{

    

    NSDateFormatter *formatter =[[NSDateFormatteralloc] init];

    NSDate *date = [NSDate date];

    [formatter setTimeStyle:NSDateFormatterMediumStyle];

    NSCalendar *calendar = [[NSCalendaralloc] initWithCalendarIdentifier:NSGregorianCalendar];

    NSDateComponents *comps = [[NSDateComponentsalloc] init];

    NSInteger unitFlags = NSYearCalendarUnit |

    NSMonthCalendarUnit |

    NSDayCalendarUnit |

    NSWeekdayCalendarUnit |

    NSHourCalendarUnit |

    NSMinuteCalendarUnit |

    NSSecondCalendarUnit;

    //int week=0;

    comps = [calendar components:unitFlags fromDate:date];

    int week = [comps weekday];

    int year=[comps year];

    int month = [comps month];

    int day = [comps day];

    //[formatter setDateStyle:NSDateFormatterMediumStyle];

    //This sets the label with the updated time.

    int hour = [comps hour];

    int min = [comps minute];

    int sec = [comps second];

    NSLog(@"week%d",week);

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

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

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

    NSLog(@"hour%d",hour);

    NSLog(@"min%d",min);

    NSLog(@"sec%d",sec);

}

原文地址:https://www.cnblogs.com/guligei/p/3162119.html