How to compare current date to previous date in iphone?
25,138
Solution 1
NSDate *today = [NSDate date]; // it will give you current date
NSDate *newDate = [dateFormatter dateWithString:@"xxxxxx"]; // your date
NSComparisonResult result;
//has three possible values: NSOrderedSame,NSOrderedDescending, NSOrderedAscending
result = [today compare:newDate]; // comparing two dates
if(result==NSOrderedAscending)
NSLog(@"today is less");
else if(result==NSOrderedDescending)
NSLog(@"newDate is less");
else
NSLog(@"Both dates are same");
got your solution from this answer How to compare two dates in Objective-C
Solution 2
Alternative to @NNitin Gohel's
answer.
Compare using NSTimeInterval
ie NSDate
timeIntervalSince1970
:
NSTimeInterval *todayTimeInterval = [[NSDate date] timeIntervalSince1970];
NSTimeInterval *previousTimeInterval = [previousdate timeIntervalSince1970];
if(previousTimeInterval < todayTimeInterval)
//prevous date is less than today
else if (previousTimeInterval == todayTimeInterval)
//both date are equal
else
//prevous date is greater than today
Author by
Honey
Updated on July 10, 2022Comments
-
Honey almost 2 years
I would like to compare the current date with another date, and if that is date is earlier than the current date, then I should stop the next action. How can I do this?
I have todays date in
yyyy-MM-dd
format. I need to check this conditionif([displaydate text]<currentdate) { //stop next action }
Here if
displaydate
is less than todays date then it has to enter that condition. -
Honey over 11 yearsI have to get todays date in yyyy-MM-dd format and for that I m doing like this : now = [NSDate date]; NSDateFormatter *df=[[[NSDateFormatter alloc]init] autorelease]; df.dateFormat = @"yyyy-MM-dd"; and now I need to compare this with a Datepicker selected date which should also be in yyyy-MM-dd format..This is what I m doing NSDateFormatter *df=[[[NSDateFormatter alloc]init] autorelease]; df.dateFormat = @"yyyy-MM-dd"; NSArray *temp=[[NSString stringWithFormat:@"%@",[df stringFromDate:DatePicker.date]] componentsSeparatedByString:@""];How can I proceed further ?
-
Honey over 11 yearsWhen I try ur code im getting err.I have to get todays date in yyyy-MM-dd format and for that Im doing like this : now = [NSDate date]; NSDateFormatter *df=[[[NSDateFormatter alloc]init] autorelease]; df.dateFormat = @"yyyy-MM-dd"; and now I need to compare this with a Datepicker selected date which should also be in yyyy-MM-dd format..This is what Im doing NSDateFormatter *df=[[[NSDateFormatter alloc]init] autorelease]; df.dateFormat = @"yyyy-MM-dd"; NSArray *temp=[[NSString stringWithFormat:@"%@",[df stringFromDate:DatePicker.date]] componentsSeparatedByString:@""];How can I proceed further?
-
Honey over 11 yearsIt is not equal to I should compare whether it is less than todays date ?
-
Honey over 11 yearshow can I compare this date with the datepickers selected date which is also in yyyy-MM-dd.I took datepickers date in NSArray .
-
Paras Joshi over 11 yearssee this answer.. stackoverflow.com/questions/949416/…
-
Paras Joshi over 11 yearsyes its also easy you have an array you can compare it with [yourArray objectAtIndex:intValue]; // here give integer value
-
Nitin Gohel over 11 yearscheck my updated answer visit the link in my answerd i defiantly sure u got your solution my frnd
-
Honey over 11 yearsIn the above answer u gave I have modified it as NSDate *newDate=[NSDate dateWithString:@"%@",[displaydate text]];but I m getting newDate as nil eventhough there is value in displaydate i.e 2011-11-02..Can u please tell me y ?
-
Honey over 11 years@Joshi I have tried this : now = [NSDate date]; NSDateFormatter *df=[[[NSDateFormatter alloc]init] autorelease]; df.dateFormat = @"yyyy-MM-dd"; How to take it in a some variable and then compare because when i see in formatter it is showing <NSDateFormatter: 0x4e5d320> and then I have date in textfield like this :2011-11-02 .How can I compare Please tell me
-
Nischal Hada almost 9 yearsIts great dude.. It helps me out
-
Krutarth Patel over 7 years@NitinGohel Great Solution