我想要工作日的日期=包含原始日期的星期五
我曾尝试使用以下编码,但它不起作用: -
Nsdate currDate;(currDate can be date)
int dayofweek; // Detect the day of week
NSString *Start_date = @"13/nov/2014";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSString *stringFromDate = [formatter stringFromDate:currDate];
NSCalendar* cal = [NSCalendar currentCalendar];
NSDateComponents* comp = [cal components:NSWeekdayCalendarUnit fromDate:Startdate]; dayofweek=[comp weekday];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *Startdate = [[NSDate alloc] init];
Startdate = [dateFormatter dateFromString:Start_date];
NSDate *fetchdate=[self firstDayOfWeekFromDate:Startdate];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSString *stringFromDate1 = [formatter stringFromDate:fetchdate];
NSString *trimmedString=[stringFromDate1 substringFromIndex:MAX((int)[stringFromDate1 length]-2, 0)];
NSString *trimmedString1=[stringFromDate substringFromIndex:MAX((int)[stringFromDate1 length]-2, 0)];
if([trimmedString1 isEqualToString:trimmedString]){
NSlog(@"YES IT IS DAY OF WEEK");
}
//***************************function
- (NSDate *)firstDayOfWeekFromDate:(NSDate *)date {
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* comps = [calendar components:NSYearForWeekOfYearCalendarUnit |NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:date];
[comps setWeekday:dayofweek]; // 2: monday
return [calendar dateFromComponents:comps];
}
答案 0 :(得分:1)
将其细分为步骤。绝对没有理由在一个功能中完成所有工作。
我假设你明年要和周五一样。 (从您删除的上一个问题开始)
首先,您需要在一年的时间内获得相同的日期。
- (NSDate *)dateOneYearFromDate:(NSDate *)date
{
NSDateComponents *components = [NSDateComponents new];
components.year = 1;
NSCalendar *calendar = [NSCalendar currentCalendar];
return [calendar dateByAddingComponents:components toDate:date options:0];
}
然后你需要弄清楚那天是哪一天......
- (NSInteger)dayOfWeekFromDate:(NSDate *)date
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSWeekdayCalendarUnit fromDate:date];
return components.weekday;
}
然后你需要找那个星期五的星期五......
- (NSDate *)fridayFromWeekOfDate:(NSDate *)date
{
NSInteger friday = 6;
NSInteger dayOfWeekFromDate = [self dayOfWeekFromDate:date];
NSInteger difference = friday - dayOfWeekFromDate;
NSDateComponents *components = [NSDateComponents new];
components.day = difference;
NSCalendar *calendar = [NSCalendar currentCalendar];
return [calendar dateByAddingComponents:components toDate:date options:0];
}
让每一步都迈出一小步......
现在你可以这样做......
- (void)someFunction
{
NSDate *someFriday = // this is your original date...
NSDate *oneYearLater = [self dateOneYearFromDate:someFriday];
NSDate *fridayOneYearFromNow = [self fridayFromWeekOfDate:oneYearLater];
}
答案 1 :(得分:0)
以下代码应该让您开始......(编码盲/未测试)
NSDate * currentDate = [NSDate date];
CFAbsoluteTime at = CFAbsoluteTimeGetCurrent();
CFTimeZoneRef tz = CFTimeZoneCopySystem();
SInt32 WeekdayNumber = CFAbsoluteTimeGetDayOfWeek(at, tz);
int currentDayOfTheWeek = (int)WeekdayNumber;
if(currentDayOfTheWeek == 5) {
//Its Friday!
return currentDate;
} else {
return [currentDate dateByAddingTimeInterval:((60 * 60 * 24) * (5 - currentDayOfTheWeek)];
}