2

I have some date in which i would like to change its week day to another day, while user do some action. When i change the week day to another day/days, the result NSDate stay in the same day of the month , so 13.4.15 will stay the same if i set the dayweek to 5 .

 NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *dateComponents = [calendar components:(NSCalendarUnitWeekday|NSCalendarUnitSecond|NSCalendarUnitMonth|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitYear|NSCalendarUnitDay) fromDate:lastdate];
    [dateComponents setWeekday:currentDay]; //set it to numbers from 0 to 7
    NSDate *date = [calendar dateFromComponents:dateComponents];
    lastdate=date;

    NSLog(@"%@",lastdate); //stay the same date of the month although i change the week day .

EDIT: removing the day unit from the component, will not solve the problem but will create a wrong month day for the new date :

NSLog(@"%@",lastdate);//gives 14.4.15

//change last date day to the new day
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComponents = [calendar components:(NSCalendarUnitWeekday|NSCalendarUnitSecond|NSCalendarUnitMonth|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitYear ) fromDate:lastdate];
[dateComponents setWeekday:currentDay];//added one day
NSDate *date = [calendar dateFromComponents:dateComponents];
lastdate=date;

NSLog(@"%@",lastdate); //gives 1.4.15
Curnelious
  • 1
  • 16
  • 76
  • 150

3 Answers3

1

Try to add NSCalendarUnitWeekOfMonth flag to component parsing. I assume that date components do not know which week should be set so weekday will be ignored.

NSDateComponents *dateComponents = [calendar components:(NSCalendarUnitSecond|NSCalendarUnitWeekOfMonth |NSCalendarUnitMonth|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitYear) fromDate:lastdate];
HereTrix
  • 1,315
  • 7
  • 13
0

I have manage to do that in 3 steps, taking the current date, get its day of month, than calculate days to add, than add them to the new date :

//get new date with the new month day
NSDate *newdate=[[NSDate date] dateByAddingTimeInterval:60*60*24* (currentDay-today) ]; //current day is the day the user chosen . today, is day of today .

//extract the new month day
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComponents = [calendar components:(NSCalendarUnitWeekday|NSCalendarUnitSecond|NSCalendarUnitMonth|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitYear|NSCalendarUnitDay ) fromDate:newdate];
long newDayOfMonth=[dateComponents day];


//create components from nob date
 NSDateComponents *nobComponents = [calendar components:(NSCalendarUnitWeekday|NSCalendarUnitSecond|NSCalendarUnitMonth|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitYear|NSCalendarUnitDay ) fromDate:lastdate];
[nobComponents setDay:newDayOfMonth];
 NSDate *date = [calendar dateFromComponents:dateComponents];
lastdate=date;




NSLog(@"final date:%@",lastdate);
Curnelious
  • 1
  • 16
  • 76
  • 150
0

I'm not sure if this fits your use case or not, but this answer shows how to create a new NSDate instance with a given weekday: https://stackoverflow.com/a/31740820/849180

Community
  • 1
  • 1
Josh Sklar
  • 461
  • 1
  • 3
  • 14