ios - 当我们过年时显示每个月的星期和日期列表

标签 ios iphone nsdate nscalendar

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy";
NSDate *date = [dateFormatter dateFromString:@"2011"];

NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[oneDayAgoComponents setMonth:0];
NSUInteger numWeekdays = [calendar maximumRangeOfUnit:NSWeekdayCalendarUnit].length;
int w = numWeekdays;
for(int currentdateindexpath=0;currentdateindexpath<=12;currentdateindexpath++)
{
    [formatter setDateFormat:@"MMMM yyyy"];

    [oneDayAgoComponents setMonth:currentdateindexpath];
    NSDate *monthAgo = [calendar dateByAddingComponents:oneDayAgoComponents
                                                 toDate:date
                                                options:0];


    NSString *montYear = [formatter stringFromDate:monthAgo];

    NSDateComponents* subcomponent = [calendar components:NSWeekdayCalendarUnit
                                                 fromDate:date];


    [subcomponent setWeek:currentdateindexpath];
    [formatter setDateFormat:@"'week'W"];
    NSString *stringFromDate = [formatter stringFromDate:monthAgo];
    // Get the weekday component of the current date
    [oneDayAgoComponents setDay: 0 - ([subcomponent weekday] - 1)];

    NSDate *beginningOfWeek = [calendar dateByAddingComponents:oneDayAgoComponents
                                                        toDate:date options:0];


    [formatter setDateFormat:@"dd/MM/yy"];
    NSString *weekstartdate = [formatter stringFromDate:beginningOfWeek];


    NSDateComponents *components =
        [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit |
                              NSDayCalendarUnit) fromDate: beginningOfWeek];

    beginningOfWeek = [calendar dateFromComponents:components];

    for (int d = 0; d <= w; d++)
    {
        NSDate *newDate1 = [beginningOfWeek dateByAddingTimeInterval:60*60*24*d];
        NSLog(@"days %@",newDate1);
  //+++++++++++++++++++ start week+++++++++++++++
    }
    [oneDayAgoComponents setDay:7 - ([subcomponent weekday])];

    NSDate *endOfWeek = [calendar dateByAddingComponents:oneDayAgoComponents
                                                  toDate:date options:0];

    [formatter setDateFormat:@"dd/MM/yy"];
    NSString *weekendtdate = [formatter stringFromDate:endOfWeek];

    NSLog(@"...>%@",[NSString stringWithFormat:@"%@ %@ to %@   %@", stringFromDate, weekstartdate, weekendtdate, montYear]);
}

如果我将年份设为 2011 年,我需要获取月、周、日列表。在相应的月份中,该特定周有多少周可用,有多少天可用以及一周的开始和结束日期需要显示的位置。但是在这段代码中,第一周的每个月只显示,我需要剩下的一周这个月...

示例输出:

days 2010-12-25 18:30:00 +0000
days 2010-12-26 18:30:00 +0000
days 2010-12-28 18:30:00 +0000
days 2010-12-29 18:30:00 +0000
days 2010-12-30 18:30:00 +0000
days 2010-12-31 18:30:00 +0000
days 2011-01-01 18:30:00 +0000
...>week1 26/12/10 to01/01/11   January 2011 
days 2011-01-25 18:30:00 +0000
days 2011-01-26 18:30:00 +0000
days 2011-01-27 18:30:00 +0000
days 2011-01-28 18:30:00 +0000
days 2011-01-29 18:30:00 +0000
days 2011-01-30 18:30:00 +0000
days 2011-01-31 18:30:00 +0000
days 2011-02-01 18:30:00 +0000
...>week1 26/01/11 to01/02/11   February 2011 

最佳答案

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
     formatter.dateFormat = @"yyyy";
    NSDate *date = [formatter dateFromString:@"2011"];

    NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
    // Create the start date components
    NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init];

    [oneDayAgoComponents setMonth:0];
    NSUInteger numWeekdays = [calendar maximumRangeOfUnit:NSWeekdayCalendarUnit].length;
   int w=numWeekdays;
    for(int currentdateindexpath=0;currentdateindexpath<=52;currentdateindexpath++)
    {

        [oneDayAgoComponents setWeek:currentdateindexpath];

        NSDate *monthAgo = [calendar dateByAddingComponents:oneDayAgoComponents
                                                     toDate:date
                                                    options:0];
        [formatter setDateFormat:@"MMMM yyyy"];
        NSString *montYear = [formatter stringFromDate:monthAgo];

         [formatter setDateFormat:@"'week'W"];
        stringFromDate = [formatter stringFromDate:monthAgo];
         NSDateComponents* subcomponent = [calendar components:NSWeekdayCalendarUnit
                                                     fromDate:date];


        [oneDayAgoComponents setDay: 0 - ([subcomponent weekday] - 1)];
        //NSLog(@"oneDayAgoComponents1 %@",oneDayAgoComponents);
        NSDate *beginningOfWeek = [calendar dateByAddingComponents:oneDayAgoComponents
                                                            toDate:date options:0];

        //NSLog(@" beginningOfWeek %@",beginningOfWeek);
        [formatter setDateFormat:@"dd/MM/yy"];
        NSString *weekstartdate = [formatter stringFromDate:beginningOfWeek];
        // NSLog(@"weekstartdate %@",weekstartdate);

        NSDateComponents *components =
        [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit |
                              NSDayCalendarUnit) fromDate: beginningOfWeek];

        beginningOfWeek = [calendar dateFromComponents:components];

        for(int d=0;d<=w;d++){
            NSDate *newDate1 = [beginningOfWeek dateByAddingTimeInterval:60*60*24*d];
             NSString *date_ = [formatter stringFromDate:newDate1];
            NSLog(@"days %@",date_);
            //NSLog(@"Show %@",beginningOfWeek);
        }
        [oneDayAgoComponents setDay:7- ([subcomponent weekday])];

        // NSLog(@"oneDayAgoComponents2 %@",oneDayAgoComponents);
        NSDate *endOfWeek = [calendar dateByAddingComponents:oneDayAgoComponents
                                                      toDate:date options:0];
        // NSLog(@" endOfWeek %@",endOfWeek);
        [formatter setDateFormat:@"dd/MM/yy"];
        NSString *weekendtdate = [formatter stringFromDate:endOfWeek];
        // NSLog(@"weekendtdate %@",weekendtdate);


        NSLog(@"...>%@",[NSString stringWithFormat:@"%@ %@ to%@   %@ ",stringFromDate,weekstartdate,weekendtdate,montYear]);
}

关于ios - 当我们过年时显示每个月的星期和日期列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21724476/

相关文章:

objective-c - 获取日期与 [NSDate date] 相差几个小时

ios - ViewController 不访问您的元素

iphone - 设置 VC.view 出现在父 VC 的 View 中,不适合我

ios - 获取 CollectionView IOS 中的列数

macos - 在 Cocoa 中安排方法调用的方法?

iphone - 离线时特定时区的iOS“实时”

ios - 如何更改我的返回方法上的可观察类型

ios - 代码 : Generating an automatic comment upon Archive

ios - 未调用handleRemoteNowPlayingActivity

iphone - 使用 iCarousel 缩放项目