Objective c 明天返回';国庆日

Objective c 明天返回';国庆日,objective-c,nsdate,nsdateformatter,Objective C,Nsdate,Nsdateformatter,我想返回明天的工作日名称(即,如果今天是星期天,我想要星期一)。这是我的代码,它产生了今天的工作日名称 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"EEEE"]; weekDay = [formatter stringFromDate:[NSDate date]]; 与其与NSCalendar混为一谈,做这件事的简明方法是什么(遵循我这里的代码) 谢谢。你为什么

我想返回明天的工作日名称(即,如果今天是星期天,我想要星期一)。这是我的代码,它产生了今天的工作日名称

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"EEEE"];
weekDay =  [formatter stringFromDate:[NSDate date]];
与其与NSCalendar混为一谈,做这件事的简明方法是什么(遵循我这里的代码)

谢谢。

你为什么害怕“搞乱”NSCalendar?要使用NSDateComponents方法,实际上必须使用NSCalendar。这是我解决你问题的办法

// Get the current weekday
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents = [calendar components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
// !! Sunday = 1
NSInteger weekday = [weekdayComponents weekday];
现在,您可以使用NSDateFormatter方法-(NSArray*)weekdaySymbols,它包含从索引0处的星期日开始的工作日。由于[weekdayComponents weekday]返回的整数在星期六以0开始,因此我们不必增加存储在weekday中的值:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// !! Sunday = 0
NSUInteger symbolIndex = (weekday < 7) ? weekday : 0;
NSString *weekdaySymbol = [[formatter weekdaySymbols] objectAtIndex:(NSUInteger)symbolIndex];
NSDateFormatter*formatter=[[NSDateFormatter alloc]init];
// !! 星期日=0
NSU整数符号索引=(工作日<7)?平日:0 ;;
NSString*weekdaySymbol=[[formatter weekdaySymbols]对象索引:(nsInteger)符号索引];
虽然在某种程度上使用了NSCalendar,但我希望这是有帮助的

编辑:glektrik的解决方案非常简单。但请注意-dateByAddingTimeInterval的NSDate类引用中的以下语句:

返回值: 相对于接收器设置为秒的新NSDate对象。返回的日期可能与接收方的表示不同

你为什么害怕“搞乱”NSCalendar?要使用NSDateComponents方法,实际上必须使用NSCalendar。这是我解决你问题的办法

// Get the current weekday
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents = [calendar components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
// !! Sunday = 1
NSInteger weekday = [weekdayComponents weekday];
现在,您可以使用NSDateFormatter方法-(NSArray*)weekdaySymbols,它包含从索引0处的星期日开始的工作日。由于[weekdayComponents weekday]返回的整数在星期六以0开始,因此我们不必增加存储在weekday中的值:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// !! Sunday = 0
NSUInteger symbolIndex = (weekday < 7) ? weekday : 0;
NSString *weekdaySymbol = [[formatter weekdaySymbols] objectAtIndex:(NSUInteger)symbolIndex];
NSDateFormatter*formatter=[[NSDateFormatter alloc]init];
// !! 星期日=0
NSU整数符号索引=(工作日<7)?平日:0 ;;
NSString*weekdaySymbol=[[formatter weekdaySymbols]对象索引:(nsInteger)符号索引];
虽然在某种程度上使用了NSCalendar,但我希望这是有帮助的

编辑:glektrik的解决方案非常简单。但请注意-dateByAddingTimeInterval的NSDate类引用中的以下语句:

返回值: 相对于接收器设置为秒的新NSDate对象。返回的日期可能与接收方的表示不同


谢谢你的评论,阿比泽恩。以下是我所做的:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"EEEE"];
    weekDay = [formatter stringFromDate:[NSDate date]];

    NSDateComponents *tomorrowComponents = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];

    NSDate *compDate = [[NSCalendar currentCalendar] dateFromComponents:tomorrowComponents];

    NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
    offsetComponents.day = 1;
    NSDate *tomorrow = [[NSCalendar currentCalendar] dateByAddingComponents:offsetComponents toDate:compDate options:0];

    nextWeekDay = [formatter stringFromDate:tomorrow];
两个NSString对象(weekDay和nextWeekDay)现在存储今天和明天(当前为星期日和星期一)的星期名称。 这很有效,但我想知道是否有更简单的方法。目标C日期相当繁琐:(


再次感谢。

谢谢你的评论,阿比泽恩。以下是我所做的:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"EEEE"];
    weekDay = [formatter stringFromDate:[NSDate date]];

    NSDateComponents *tomorrowComponents = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];

    NSDate *compDate = [[NSCalendar currentCalendar] dateFromComponents:tomorrowComponents];

    NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
    offsetComponents.day = 1;
    NSDate *tomorrow = [[NSCalendar currentCalendar] dateByAddingComponents:offsetComponents toDate:compDate options:0];

    nextWeekDay = [formatter stringFromDate:tomorrow];
两个NSString对象(weekDay和nextWeekDay)现在存储今天和明天(当前为星期日和星期一)的星期名称。 这很有效,但我想知道是否有更简单的方法。Objective-C日期相当麻烦:(

再次感谢。

用户语言感知:

NSDateFormatter * df = [[NSDateFormatter alloc] init];
NSArray *weekdays = [df weekdaySymbols];

NSDateComponents *c = [[NSDateComponents alloc] init];
c.day = 1;
NSDate *tomorrow = [[NSCalendar currentCalendar] dateByAddingComponents:c
                                                                 toDate:[NSDate date]
                                                                options:0];
c = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit
                                    fromDate:tomorrow];

NSString *tomorrowname = weekdays[c.weekday-1];// the value of c.weekday may 
                                               // range from 1 (sunday) to 7 (saturday)    
NSLog(@"%@", tomorrowname);

如果需要使用某种语言命名,请添加

[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
创建日期格式化程序后。

用户语言感知:

NSDateFormatter * df = [[NSDateFormatter alloc] init];
NSArray *weekdays = [df weekdaySymbols];

NSDateComponents *c = [[NSDateComponents alloc] init];
c.day = 1;
NSDate *tomorrow = [[NSCalendar currentCalendar] dateByAddingComponents:c
                                                                 toDate:[NSDate date]
                                                                options:0];
c = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit
                                    fromDate:tomorrow];

NSString *tomorrowname = weekdays[c.weekday-1];// the value of c.weekday may 
                                               // range from 1 (sunday) to 7 (saturday)    
NSLog(@"%@", tomorrowname);

如果需要使用某种语言命名,请添加

[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];

创建日期格式化程序后。

NSDateComponents有帮助的方法。获取日期获取第二天。如果您不太关心闰小时等,您可以始终运行上述+24小时。DateWithTimeIntervalencesInnow.NSDateComponents有帮助的方法。获取日期获取第二天。您可以始终运行above+24小时,如果你不太关心闰小时等。DateWithTimeIntervalencesInnow。是的,复制和粘贴错误:我从代码中获取,在那里我演示了如何获取前n个用户的语言。是的,复制和粘贴错误:我从代码中获取,在那里我演示了如何获取前n个用户的语言。你不必添加日期s、 您只需增加当前
NSDateComponents
日数
。无论框架如何,日期处理都很麻烦;
NSDateFormatter
NSCalendar
已经隐藏了堆积如山的复杂性。如果您经常需要此功能,请将其放入自己的库中。您不必添加日期;您可以现在的
NSDateComponents
只需增加
day
。无论框架如何,日期处理都很麻烦;
NSDateFormatter
NSCalendar
已经隐藏了大量的复杂性。如果您经常需要此功能,请将其放入自己的库中。是和否![formatter weekdaySymbols]返回(星期日,星期一,…),因此,星期日将通过此处的索引0访问。如上所述,如果使用[weekdayComponents weekday],则星期日表示为1。是和否![formatter weekdaySymbols]返回(星期日,星期一,…),因此,这里通过索引0访问星期日。如上所述,如果使用[weekdayComponents weekday],星期日将表示为1。