简体   繁体   English

如何在目标C中从今天开始一周获得几天

[英]How to get days in a week from today in objective c

I am trying to get the days of a week in reference to their date. 我想参考一周的日期。 Ex.: 14th October is a Wednesday. 例如:10月14日是星期三。 I am creating a collection view cell and displaying the date as follows. 我正在创建一个收集视图单元格,并按如下所示显示日期。 在此处输入图片说明

In the 14th cell it would display Wednesday, but how to get the next day as Thursday, then Friday...and so on. 在第14个单元格中,它将显示星期三,但如何将第二天显示为星期四,然后显示星期五……等等。

I have got todays date and which day it is as follows, 我有今天的日期,如下所示。

day = [components day];
week = [components month];
year = [components year];
weekday = [components weekday];
NSString *string = [NSString stringWithFormat:@"%ld.%ld.%ld", (long)day, (long)week, (long)year];
NSLog(@"%@",string);
NSDate *todayDate = [NSDate date]; // get today date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEEE"];
NSString *convertedDateString = [dateFormatter stringFromDate:todayDate];// here convert date in
NSLog(@"Today formatted date is %@",convertedDateString);

And i have found this method online, which returns the days in string when you pass the integer, but i am having trouble in this too, not understanding which integer to pass every time so I get different strings. 而且我在网上找到了此方法,当您传递整数时,该方法返回字符串中的天数,但是我也遇到了麻烦,无法每次都知道要传递哪个整数,所以我得到了不同的字符串。

static inline NSString *stringFromWeekday(int weekday){
static NSString *strings[] = {
    @"Sunday",
    @"Monday",
    @"Tuesday",
    @"Wednesday",
    @"Thursday",
    @"Friday",
    @"Saturday",
};

return strings[weekday];

} }

You already got everything in place. 您已经准备就绪。 You should be getting the correct day name in the convertedDateString string. 你应该在得到正确的日名称convertedDateString字符串。 You just have to add 24 hours to your todayDate object and use the dateformatter again to get the next day. 您只需将24小时添加到todayDate对象中,然后再次使用dateformatter获取第二天。

NSDate *todayDate = [NSDate date]; // get today date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEEE"];
NSString *convertedDateString = [dateFormatter stringFromDate:todayDate];// here convert date in

You can get the next day like this. 你可以这样得到第二天。

NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:(NSDayCalendarUnit | NSMonthCalenderUnit | NSYearCalenderUnit | NSWeekdayCalendarUnit) fromDate:today];
[dateComponents setDay:dateComponents.day+1];
NSDate *nextDay = [dateComponents date];
// Get the day by following same approach in the top

You will get days in a week from today using below code. 使用下面的代码,您将从今天开始在一周之内得到几天。

NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents =
                [gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:today];
NSInteger day = [weekdayComponents day];
NSInteger weekday = [weekdayComponents weekday];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM