码迷,mamicode.com
首页 > 移动开发 > 详细

ios获取当前年月日和星期

时间:2015-06-13 18:35:44      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:

    //获取日期

    NSArray * arrWeek=[NSArray arrayWithObjects:@"星期日",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六", nil];

    NSDate *date = [NSDate date];

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

    NSDateComponents *comps = [[NSDateComponents alloc] init] ;

    NSInteger unitFlags = NSYearCalendarUnit |

    NSMonthCalendarUnit |

    NSDayCalendarUnit |

    NSWeekdayCalendarUnit |

    NSHourCalendarUnit |

    NSMinuteCalendarUnit |

    NSSecondCalendarUnit;

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

    int week = [comps weekday];

    int year=[comps year];

    int month = [comps month];

    int day = [comps day];

    NSLog(@"%@",[NSString stringWithFormat:@"%d年%d月",year,month]);

    NSLog(@"%@",[NSString stringWithFormat:@"%d",day]);

    NSLog(@"%@",[NSString stringWithFormat:@"%@",[arrWeek objectAtIndex:week]]);

ios获取当前年月日和星期

标签:

原文地址:http://www.cnblogs.com/tate-zwt/p/4573855.html

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