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

IOS 获取年月日、星期

时间:2015-04-23 13:30:35      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:ios   nsdatecomponents   nsdateformatter   年月日   星期   

#pragma mark - 获取年月日及星期
+ (NSString *)stringFromDate
{
    // 添加日期
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
    NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    
    NSDate *  senddate=[NSDate date];
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"YYYY-MM-dd"];
    comps = [calendar components:unitFlags fromDate:senddate];
    NSString *locationString=[dateformatter stringFromDate:senddate];
    NSString *date_weekStr = [NSString stringWithFormat:@"%@ %@",locationString,[self week:[comps weekday]]];
    return date_weekStr;
}

#pragma mark - 推算星期
+(NSString *)week:(NSInteger)week
{
    NSString *weekStr=nil;
    if(week==1)
    {
        weekStr=@"星期天";
    }else if(week==2){
        weekStr=@"星期一";
        
    }else if(week==3){
        weekStr=@"星期二";
        
    }else if(week==4){
        weekStr=@"星期三";
        
    }else if(week==5){
        weekStr=@"星期四";
        
    }else if(week==6){
        weekStr=@"星期五";
        
    }else if(week==7){
        weekStr=@"星期六";
        
    }
    return weekStr;
}

IOS 获取年月日、星期

标签:ios   nsdatecomponents   nsdateformatter   年月日   星期   

原文地址:http://blog.csdn.net/missafeng/article/details/45218951

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