gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 04:41:44 26 4
gpt4 key购买 nike

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com