Reputation: 8790
I want to check for the date and get the result in 4 Format TheDayBeforeYesterday,Yesterady,Today,Tomorrow.
I have One date and i want to compare that date with the current Date.
Right now i have done this but this is just give me the Day in positive number so i cant get the date is gone or will it still to come that date.
-(void)getDateDiffrenceForBirthday:(NSString *)strOriginalDate
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *originalDate = [formatter dateFromString:strOriginalDate];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSDayCalendarUnit
fromDate:originalDate
toDate:[NSDate date]
options:0];
NSLog(@"Days To Go %d",components.day);
}
Upvotes: 1
Views: 192
Reputation: 414
You may use this function. It formats argument date as follows:
if date is tomorrow, today, yesterday, day before yesterday, it returns corresponding text
if date is less than a week ago, returns week day, day and month
all other dates formatted in medium style
NSString* dateFormatted(NSDate* datetime){
if(datetime==nil){return nil;}
NSDate *todayWithTime=[NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *todayComponents=[gregorian components: (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate: todayWithTime];
NSDate *tomorrow=[[NSDate alloc] initWithTimeInterval:86399 sinceDate:[gregorian dateFromComponents: todayComponents]];
NSDateComponents * interval = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:datetime toDate:tomorrow options:0];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:CurrentLocale()];
NSString *result;
if((interval.year==0)&&(interval.month==0)){
if(interval.day==-1){
result=NSLocalizedString(@"Tomorrow", @"");
}else if(interval.day==0){
result=NSLocalizedString(@"Today", @"");
}else if(interval.day==1){
result=NSLocalizedString(@"Yesterday", @"");
}else if(interval.day==2){
result=NSLocalizedString(@"The Day Before Yesterday", @"");
}else if(interval.day<7){
[dateFormatter setDateFormat:@"EEEE, d MMMM"];
result = [dateFormatter stringFromDate:datetime];
}else{
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
result = [dateFormatter stringFromDate:datetime];
}
}else{
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
result = [dateFormatter stringFromDate:datetime];
}
return result;
}
Upvotes: 2
Reputation: 8444
You can try the below code to get the date difference
-(void)getDateDiffrenceForBirthday:(NSString *)strOriginalDate
{
NSDate *originalDate = [formatter dateFromString:strOriginalDate];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSUInteger unitFlags = NSDayCalendarUnit;
NSDateComponents *components = [gregorian components:unitFlags fromDate:originalDate toDate:[NSDate date] options:0];
NSLog(@"Days To Go %d",[components day]+1);
}
Upvotes: 1