本文介绍了objective-c:将日期字符串转换为星期几+星期名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 初学者的问题,但我想知道是否有人可以帮助我:A beginner's problem, but I was wondering if anyone could help me with this:我需要根据包含某个日期的字符串设置四个字符串例如@2011年4月7日):I need to set four strings according to a string which contains a certain date (e.g. @"Apr 7, 2011"): 一个字符串,将采取一周中的某一天(缩写:Mon,Tue,Wed ,Thu,Fri,Sat,Sun):例如, @Thu 一个字符串, @7 一个字符串, @April 和一个字符串, @2011a string which will take the day of the week (abbreviated: Mon, Tue, Wed, Thu, Fri, Sat, Sun): e.g. @"Thu"a string which will take the day, e.g. @"7"a string which will take the month, e.g. @"April"and a string which will take the year, e.g. @"2011"到目前为止,我发现这个:So far, I found this: NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];[dateFormatter setTimeStyle:NSDateFormatterNoStyle];[dateFormatter setDateStyle:NSDateFormatterMediumStyle];NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:118800];NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];[dateFormatter setLocale:usLocale];NSLog(@"Date for locale %@: %@", [[dateFormatter locale] localeIdentifier], [dateFormatter stringFromDate:date]);// Output:// Date for locale en_US: Jan 2, 2001 所以这会给我一个格式化日期。我想知道,但是,我怎么能访问这个日期的某些部分。有 - (NSArray *)weekdaySymbols但我不知道如何工作这一个和文档是相当节俭。So this would give me a certain formatted date. I was wondering, however, how I can access certain parts of this date. There is the - (NSArray *)weekdaySymbols but I have no idea how to work this one and the documentation is quite frugal.日历专家的任何提示都将受到欢迎。Any hints from calendar experts would be very welcome. EDIT:我想这是解决方案的一部分:I guess this is part of the solution: NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];NSString *gbFormatString = [NSDateFormatter dateFormatFromTemplate:@"EdMMM" options:0 locale:gbLocale];NSLog(@"gbFormatterString: %@", gbFormatString);// Output: gbFormatterString: EEE d MMM, e.g. Thu 7 Apr推荐答案 n.evermind,n.evermind,您将需要这样的: NSDate *date = [NSDate date]; NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease]; [formatter setDateFormat:@"MMM dd, yyy"]; date = [formatter dateFromString:@"Apr 7, 2011"]; NSLog(@"%@", [formatter stringFromDate:date]); NSCalendar *calendar = [NSCalendar currentCalendar]; NSInteger units = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit; NSDateComponents *components = [calendar components:units fromDate:date]; NSInteger year = [components year]; NSInteger month=[components month]; // if necessary NSInteger day = [components day]; NSInteger weekday = [components weekday]; // if necessary NSDateFormatter *weekDay = [[[NSDateFormatter alloc] init] autorelease]; [weekDay setDateFormat:@"EEE"]; NSDateFormatter *calMonth = [[[NSDateFormatter alloc] init] autorelease]; [calMonth setDateFormat:@"MMMM"]; NSLog(@"%@ %i %@ %i", [weekDay stringFromDate:date], day, [calMonth stringFromDate:date], year );输出2011-04-07 12:49:23.519 test[7296:207] Apr 07, 20112011-04-07 12:49:23.521 test[7296:207] Thu 7 April 2011乔丹,乔丹 这篇关于objective-c:将日期字符串转换为星期几+星期名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-28 20:06