iOS 开发 时间格式字符串转化为周几的方法

2016-12-06  本文已影响0人  忆风_

一、例如:

               NSString *ceShiStr = [self getWeekDay:@"2016-12-04"];

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

               打印结果 : 周日

- (NSString*)getWeekDay:(NSString*)currentStr

{

NSDateFormatter* dateFormat = [[NSDateFormatteralloc]init];//实例化一个NSDateFormatter对象

[dateFormatsetDateFormat:@"yyyy-MM-dd"];//设定时间格式,要注意跟下面的dateString匹配,否则日起将无效

NSDate*date =[dateFormatdateFromString:currentStr];

NSArray*weekdays = [NSArrayarrayWithObjects: [NSNullnull],@"周日",@"周一",@"周二",@"周三",@"周四",@"周五",@"周六",nil];

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

NSTimeZone*timeZone = [[NSTimeZonealloc]initWithName:@"Asia/Shanghai"];

[calendarsetTimeZone: timeZone];

NSCalendarUnitcalendarUnit =NSCalendarUnitWeekday;

NSDateComponents*theComponents = [calendarcomponents:calendarUnitfromDate:date];

return[weekdaysobjectAtIndex:theComponents.weekday];

}

上一篇下一篇

猜你喜欢

热点阅读