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

timestr

时间:2016-03-30 12:28:35      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

-(void)timeStr

{

    NSDate *date = [NSDate date];

    NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;

    NSCalendar *currentCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

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

    formatter.dateFormat = @"yyyy.MM";

    NSDateComponents *currentComps = [currentCalendar components:unitFlags fromDate:date];

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

    [resultComps setYear:[currentComps year]];

    [resultComps setMonth:[currentComps month]];

    [resultComps setWeekday:[currentComps weekday]];

    NSInteger mMonth = [currentComps month];

    for(int i =3;i>-1;i--){

        NSInteger uMonth = mMonth - i;

        [resultComps setMonth:uMonth];

        NSDate *returnDate = [currentCalendar dateFromComponents:resultComps];

        NSString *returnStr = [formatter stringFromDate:returnDate];

        NSLog(@"%@",returnStr);

    }

    

}

 

2月份的   30日   =  3月份,我日错了

timestr

标签:

原文地址:http://www.cnblogs.com/guligei/p/5336571.html

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