Satrt date and end date validation - iphone

I have start date and end date in string format.i want to compare end date and start date,end date should be greater than start date.How to do this.
NSLog(#"%#",date);
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"dd MM yyyy"];
_start_date = [[NSDate alloc] init];
_start_date = [df dateFromString: date];
NSLog(#"date: %#", _start_date);`
Getting nil on start_date.

You need to convert both the strings to NSDate objects using NSDateFormatter.
As your date is in dd-MM-yy format, do as shown below :
NSString *dateStr1 = #"21-3-14";
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:#"dd-MM-yy"];
NSDate *date1 = [dateFormatter dateFromString:dateStr1];
Similarly create another date, say date2 Then you can compare these dates.
if ([date1 compare:date2] == NSOrderedDescending) {
NSLog(#"date1 is later than date2");
} else if ([date1 compare:date2] == NSOrderedAscending) {
NSLog(#"date1 is earlier than date2");
} else {
NSLog(#"dates are the same");
}

Create an instance of NSDateFormatter.
Configure it.
Parse the strings to get NSDate objects (dateFromString:).
Compare them (compare:).

Related

how to compare two NSString which have date

I have two NSString which have date i want that if they both have same data then if should work otherwise else should work
here is my code
NSString*test=data.addedDateTime;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy_MM_dd"];
NSDate* date = [NSDate date];
NSString* str = [formatter stringFromDate:date];
NSString*addedDateTime=str;
if ([test isEqualToString:str]) {
[todayArray addObject:theObject];
int count=[todayArray count];
NSLog(#"Today array working %d",count);
}
else {
[yesterdayArray addObject:theObject];
}
my code always runs else when there is same value in both i have checked using NSLog
Don't compare two NSStrings, which represent the dates, but compare two NSDates instead
First you convert string to date and use this [date1 isEqualToDate:date2].This is a compair a two date.
use this code I hope this code useful for you.
NSString*test=data.addedDateTime;
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy_MM_dd"];
NSDate* d = [df dateFromString:test];
[df release];
NSDate* date = [NSDate date];
if ([d isEqualToDate:date) {
[todayArray addObject:theObject];
int count=[todayArray count];
NSLog(#"Today array working %d",count);
}
else {
[yesterdayArray addObject:theObject];
}
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *date1 = [dateFormatter dateFromString:TodatDate];
NSDateFormatter *dateFormatte = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatte setDateFormat:#"yyyy-MM-dd"];
NSDate *date2 = [dateFormatte dateFromString:ServerDate];
unsigned int unitFlags = NSDayCalendarUnit;
NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [gregorian components:unitFlags fromDate:date1 toDate:date2 options:0];
int days = [comps day];
NSLog(#"%d",days);
Condition [test isEqualToString:str] always gives NO if date is either not equal or in not same formate. So dont use this.
Use NSDateComponent to compare two dates or Compare as #Andrey Gordeev
Says .
Try this code for any comparison between dates... You should not compare date in the form of string. Compare the dates before conversion to string. Convert the string "test" into date format using dateFromString function of the formatter by specifying the exact date format as that of the test. Then compare the dates using following function.
NSString*test=data.addedDateTime;
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"yyyy_MM_dd"];
NSDate *newDate = [formatter dateFromString:data.addedDateTime]; // other date
NSDate *today = [NSDate date]; // current date
NSComparisonResult result;
result = [today compare:newDate]; // comparing two dates
if(result == NSOrderedSame)
{
[todayArray addObject:theObject];
int count=[todayArray count];
NSLog(#"Today array working %d",count);
}
else
{
[yesterdayArray addObject:theObject];
}
Instead of comparing String compare Date. Like this,
NSDate *myDate1 =data.addedDateTime;
NSDate* mydate2 = [NSDate date];
if ([myDate1 compare:mydate2] == NSOrderedDescending) {
NSLog(#"Dilip myDate1 is later than myDate2");
} else if ([myDate1 compare:mydate2] == NSOrderedAscending) {
NSLog(#"Dilip myDate1 is earlier than myDate2");
} else {
NSLog(#"Dilip dates are the same");
}
first u should convert date to nsstring..
then u set perfect date formate in both string..
like
NSDate *today1 = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd-MM-yyyy"];
NSString *dateString11 = [dateFormat stringFromDate:today1];
[dateString11 retain];
ur first string is like dateString11
and other date string is like str2=#"03-10-2010";
then u compare two string
if ([dateString11 isEqualToString:str2]) {
[todayArray addObject:theObject];
int count=[todayArray count];
NSLog(#"Today array working %d",count);
}

Conversion of NSString into the NSDate is giving wrong value and format in iPhone

I want to compare two dates.For that i have to dates in two strings(fromDates and toDates).
here fromDate is showing the correct value in console.
//Console.
19/12/2012 from date is
After the NSDateFormatter process im getting wrong format and date and sometime it is showing NULL.
//console
First = (null)
I used the below method.
if (fromDate) {
self.printFromDate.text = [NSString stringWithFormat:#"%#",curBtn.dateString];
fromDates = self.printFromDate.text;
NSLog(#"%# from date is",fromDates);
dtFormatter = [[NSDateFormatter alloc] init];
[dtFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *strDate = [dtFormatter dateFromString:fromDates];
NSLog(#"First = %#",strDate);
}
Can anyone please tell me where im goin wrong.i completly stuck here.Thanks in advance.
Your date formatter doesn't follow the string format.
I think you're looking for this:
[dtFormatter setDateFormat:#"dd/MM/yyyy"];
NSDate *date1 = [dtFormatter dateFromString:fromDates];
NSDate *date2 = [dtFormatter dateFromString:anotherDate];
if ([date1 compare:date2] == NSOrderedDescending)
NSLog(#"date1 comes first");
else if ([date1 compare:date2] == NSOrderedAscending)
NSLog(#"date2 comes first");
else
NSLog(#"date1 is the same date as date2");
dtFormatter = [[NSDateFormatter alloc] init];
[dtFormatter setDateFormat:#"dd/MM/yyyy"];
NSDate *strDate = [[NSDate alloc] init];
strDate = [dtFormatter dateFromString:fromDates];
NSLog(#"First = %#",strDate);
I think it will be helpful to you.

Date and Time comparison including AM/PM in iphone sdk

I need to compare two dates with time (am/pm). I used the following code.
-(NSDate*)dateFromString:(NSString*)dateString{
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd H:mm:ss"];
NSDate *date = [dateFormat dateFromString:dateString];
[dateFormat release];
return date;
}
#define kMinute 60
#define kHour (60*60)
#define kDay (60*60*24)
-(NSString*)textFromSeconds:(double)seconds{
if(seconds < kMinute)
return [NSString stringWithFormat:#"%0.0f seconds",seconds];
if(seconds < kHour)
return [NSString stringWithFormat:#"%0.0f minutes",seconds/kMinute];
if(seconds < kDay)
return [NSString stringWithFormat:#"%0.0f hours",seconds/kHour];
return [NSString stringWithFormat:#"%0.0f days", seconds/kDay];
}
-(NSString*)textFromDateString:(NSString*)dateString{
NSDate *date =[self dateFromString:dateString];
double seconds = [[NSDate date] timeIntervalSinceDate:date];
NSString *text = [self textFromSeconds:seconds];
NSLog(#"text is: %#", text);
return text;
}
-(void)test{
[self textFromDateString:#"2011-02-20 17:19:25"];
[self textFromDateString:#"2011-02-20 17:10:25"];
[self textFromDateString:#"2011-02-20 14:04:25"];
[self textFromDateString:#"2011-02-19 14:04:25"];
}
Its working perfect. But I need to convert this into 12 hour clock. So that I need to compare two dates with AM or PM. What modifications should I do in the above code to achieve the goal?
I changed the dateformat to #"yyyy-MM-dd HH:mm a" and tried with date in the same format, But its not working.
Thanks.
Try changing the dateformat to #"yyyy-MM-dd hh:mm a"

How to sort NSArray with date time values?

I have an NSArray containing date/time NSStrings in the following format:
2/2/2011 2:46:39 PM
2/4/2011 11:59:47 AM
…
where the date is represented as month/day/year.
How do I sort this NSArray making sure the newest date/times are at the top?
When you’re dealing with dates, use NSDate instead of NSString. Also, it’s important to consider the time zone — does the Web service provide dates in UTC or some other time zone?
You should first convert your array of strings into an array of dates. Otherwise, you’d be converting a string to a date whenever it is used for comparison, and there will be more comparisons than the number of strings.
For example:
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"MM/DD/YYYY hh:mm:ss a"];
NSMutableArray *dateArray = [NSMutableArray array];
for (NSString *dateString in array) {
NSDate *date = [formatter dateFromString:dateString];
if (date) [dateArray addObject:date];
// If the date is nil, the string wasn't a valid date.
// You could add some error reporting in that case.
}
This converts array, an array of NSStrings, to dateArray, a mutable array of NSDates. The date formatter uses the system time zone. If you want to use UTC as the time zone:
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"MM/DD/YYYY hh:mm:ss a"];
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
Having done that, sorting the array is trivial:
[dateArray sortUsingSelector:#selector(compare:)];
Use method compare to compare two dates,
Sample NSDate comparision,
NSDate *dateOne = [NSDate dateWithString:#"2008-12-04 03:00:00 +0900"];
NSDate *dateTwo = [NSDate dateWithString:#"2008-12-04 04:00:00 +0900"];
switch ([dateOne compare:dateTwo]){
case NSOrderedAscending:
NSLog(#”NSOrderedAscending”);
break;
case NSOrderedSame:
NSLog(#”NSOrderedSame”);
break;
case NSOrderedDescending:
NSLog(#”NSOrderedDescending”);
break;
}
Use you own logic to sort
use this
NSMutableArray *mutArr=[yourArray mutableCopy];//convert your NSArray into mutable array
NSDateFormatter *df=[[[NSDateFormatter alloc] init] autorealese];
[df setDateFormat:#"MM/DD/YYYY hh:mm:ss a"];
NSDate *compareDate;
NSInteger index;
for(int i=0;i<[mutArray count];i++)
{
index=i;
compareDate=[df dateFromString:[mutArray objectAtIndex:i]];
NSDate *compareDateSecond;
for(int j=i+1;j<counter;j++)
{
compareDateSecond=[df dateFromString:[mutArr objectAtIndex:j]];
NSComparisonResult result = [compareDate compare:compareDateSecond];
if(result == NSOrderedAscending)
{
compareDate=compareDateSecond;
index=j;
}
}
if(i!=index)
[mutArr exchangeObjectAtIndex:i withObjectAtIndex:index];
}
}
NSLog(#"%#",mutArr);

NSDateFormatter return null

I'm trying to format a NSDate to a string with date format of yyyyMMdd, here's the code
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyyMMdd"];
NSDate *date =data.createdTime;
NSLog(#"%#",date);
NSLog(#"%#",[dateFormatter stringFromDate:date]);
the NSLog return me this value
Tue, 24 May 2011 0:05:01 +0800
(null)
Anyone know which part is wrong?
Thanx in advance.
Perhaps currentArticle.createdTime is a valid date but data.createdTime is nil? In addition, you are performing the -setDateFormat: selector on dateFormat, but it seems like your date formatter is dateFormatter.
Try the following code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyyMMdd"];
NSDate *date = data.createdTime;
NSLog(#" Normal Date = %#", date);
NSLog(#"Formatted Date = %#", [dateFormatter stringFromDate: date]);
[dateFormatter release];
If date is nil, then both NSLog() calls will tell you.
Edit
Double check that data.createdTime is an NSDate instance. Perhaps it is an instance of another class, such as NSString, whose -description returns the displayed date. This would explain why NSLog() “shows” the date, but the formatter is returning nil.
BOOL isDate = [data.createdTime isKindOfClass: [NSDate class]];
NSLog(#"Date %# a date.", isDate ? #"is" : #"is not");
[dateFormat setDateFormat:#"yyyyMMdd"];
Should be:
[dateFormatter setDateFormat:#"yyyyMMdd"];
If it still isn't working then something is wrong with data.createdTime; because I ran this code and recieved the expected output:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyyMMdd"];
NSDate *date = [NSDate date];
NSLog(#"%#",date);
NSLog(#"%#",[dateFormatter stringFromDate:date]);