Problem in dateFormatter in Iphone sdk? - objective-c

Actually My code is:
else if(isExpense == YES)
{
NSDate *strDate;
strDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
if([expense.date length] > 0)
{
NSDate *date = [dateFormatter dateFromString:expense.date];
datePicker.date = date; //Here date value is passing nil and Im getting Exception
}
else
{
datePicker.date = strDate;
}
}
Guys can anyone help to get out of this problem?????
Anyone's help will be much appreciated.
Thank You,
Kiran.

What type is expense.date? Because NSDate doesn't have a length method. And if expense.date is a NSString
[dateFormatter dateFromString:expense.date];
will not return a valid date, because it expects a NSDate as parameter.

Related

UIDatePicker Order Format

I am new to ios development tried UIDatepicker format like day month date. I tried alot but not able to get, Can any one help ?
NSDate *storedDate = [[NSUserDefaults standardUserDefaults]objectForKey:#"DatePickerViewController.selectedDate"];
storedDate = [dateFormat dateFromString:#"EE,MMMM dd"];
// add this check and set
if (storedDate == nil) {
storedDate = [NSDate date];
}
[self.pickerView setDate:storedDate animated:NO];
i need the above image format
First, get the saved date in string Format.
NSString *storedDate = [[NSUserDefaults standardUserDefaults]objectForKey:#"DatePickerViewControllerselectedDate"];
Then get the format in which the date is stored so that you can convert it to date.
NSDateFormatter *format = [[NSDateFormatter alloc] init];
format.dateFormat=#"EEEE, MMMM dd";
Now you can convert the stored-date to date format and set in datepicker.
NSDate *sortedDateFormatted = [format dateFromString:storedDate];
[self.pickerView setDate:sortedDateFormatted animated:NO];
You need the date format ryt, Just try this
NSDate *now = [NSDate date];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
format.dateFormat=#"EEEE, MMMM dd";
NSString * dateStr = [[format stringFromDate:now] capitalizedString];
NSDate *sortedDateFormatted = [format dateFromString:dateStr];
[self.pickerView setDate:sortedDateFormatted animated:NO];

How do you compare date objects to strings in Objective-C?

I have an array of dates in this format:
date: 2012-01-02,2012-03-17,2012-04-09,2012-05-07,2012-06-04,2012-08-06,2012-10-29,2012-12-25,2012-12-26.
I want to compare the dates with today's date, but I need some help. This is my code.
NSArray *date =[dict12 objectForKey:#"ie_date_closed"];
NSLog(#"date:%#",date);
int i=date;
for (i=0; i<6; i++)
{
NSComparisonResult result = [todaydate compare:date[i]];
NSLog(#"result:%d",result);
}
Try below code:-
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:#"yyyy-MM-dd"];
for (NSString *arrDt in date){
if ([arrDt isEqualToString:[formatter stringFromDate:[NSDate date]]]){
NSLog(#"Equal Date");}}
Note:- You need to set the dateFormat according to array object dates.
Here is the code have a look at & try to implement your self using it :
for (i=0; i<[date count]; i++)
{
NSDateformatter *format = Define Your date Format Here
NSDate yourDate = [format dateFromString:[date objectAtIndex:i]];
If ([YourDate compare:[NSDate date]] == NSOrderedSame)
{
NSLog(#"Both Dates are same");
}
}
Note : I am not giving you whole readymade code but just for a hint so using it you can be able to implement what you want.
Hope this will help.
try it
NSDate *firstTime;
NSDate *nowTime;
NSDate *localDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
dateFormatter.dateFormat = #"YYYY-MM-dd";
NSString *str = [dateFormatter stringFromDate:localDate];
NSArray *date =[dict12 objectForKey:#"ie_date_closed"];
NSLog(#"date:%#",date);
for (i=0; i<[date count]; i++)
{
NSString *openTime =[NSString stringWithFormate:"%#"[date objectAtindex:i]];
NSDateFormatter *dateComperFormatter1 = [[NSDateFormatter alloc] init];
[dateComperFormatter1 setTimeZone:[NSTimeZone systemTimeZone]];
[dateComperFormatter1 setDateFormat:#"YYYY-MM-dd"];
firstTime = [dateComperFormatter1 dateFromString:openTime];
nowTime = [dateComperFormatter1 dateFromString:str];
// NSComparisonResult result;
// // has three possible values: NSOrderedSame,NSOrderedDescending, NSOrderedAscending
//
// result = [nowTime compare:firstTime]; // comparing two dates
NSComparisonResult result = [nowTime compare:firstTime];
}

"NSDate not an Objective-C object" error shows when run the application in IOS 7 in real device

NSDate object is working for iPhone, iPad when i ran the application in real devices. But when I run the application in iPad real device than it gives <not an Objective-C object> error. I tried to sort out it. but couldn't.
- (NSString*)getDateFromJSONToStringSaveFormat:(NSString *)dateString
{
NSDate *_Date = [NSDate alloc] init];
NSDate *_Date = [self getDateFromJSON:dateString];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
return [dateFormatter stringFromDate:_Date];
}
- (NSDate*) getDateFromJSON:(NSString *)dateString
{
// Expect date in this format "/Date(1268123281843)/"
int startPos = [dateString rangeOfString:#"("].location+1;
int endPos = [dateString rangeOfString:#")"].location;
NSRange range = NSMakeRange(startPos,endPos-startPos);
unsigned long long milliseconds = [[dateString substringWithRange:range] longLongValue];
NSTimeInterval interval = milliseconds/1000;
return [NSDate dateWithTimeIntervalSince1970:interval];
}
because of this issue i initialize the NSDate object and saw the date value. (NSDate *_Date = [NSDate alloc] init];) in here also gives same error? why is that? anyone faced this error ??
First off you can just remove this line:
NSDate *_Date = [NSDate alloc] init];
Since the next line just redeclares it, also you in the line you should remove you are missing a [.
- (NSString*)getDateFromJSONToStringSaveFormat:(NSString *)dateString
{
// Not needed since the line after it also declares the variable.
//NSDate *_Date = [NSDate alloc] init];
NSDate *_Date = [self getDateFromJSON:dateString];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
return [dateFormatter stringFromDate:_Date];
}

objective-c NSDateFormatter date from string returns null

I'm trying to convert this string #"August 8, 2013" to a NSDate but I keep getting NULL as my result:
strToConvert = #"August 8, 2013";
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/YYYY zzz"];
NSDate* myDate = [dateFormatter dateFromString:strToConvert];
Am I formatting it incorrectly?
Edit:
I changed the method to this:
- (NSDate*) convertStringToDate : (NSString*) strToConvert {
NSError* error = nil;
NSDataDetector* detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeDate error:&error];
NSArray* arrDateMatches = [detector matchesInString:strToConvert options:0 range:NSMakeRange(0, [strToConvert length])];
for (NSTextCheckingResult* match in arrDateMatches) {
strToConvert = [self convertDateToString:match.date];
}
NSDate* myDate = [dateFormatter dateFromString:strToConvert];
return myDate;
}
and it works fine. The only issue is I am getting a implicit conversion from enumeration type 'enum NSTextCheckingType'... any idea of how to fix that warning?
The NSDateFormatter has the incorrect format. You need to match the format of the expected input:
[dateFormatter setDateFormat:#"MMMM d, yyyy"];

Check If Date Is Between Two Dates

Good afternoon,
How do you check if a date is between two dates? I know I have to convert the data strings to NSDate values first:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
NSDate *date1 = [dateFormatter dateFromString:#"01/01/2001"];
NSDate *date2 = [dateFormatter dateFromString:#"01/01/2010"];
NSDate *userDate = [dateFormatter dateFromString:#"12/12/2001"];
And then I have to use an if-statement, but I am not quite sure how to go about it. Here is what I need:
if (userDate is between date1 and date2)
{
}
Any help with the if-statement would be appreciated. Thank you!
Use NSDate -compare::
if (([date1 compare:userDate] == NSOrderedAscending) && ([date2 compare:userDate] == NSOrderedDescending)) {
// Do something
}
of the top of my head so check the syntax ;-)
if (([userDate laterDate: date1] == userDate) && ([userDate laterDate: date2] == date2)){
}
if(([userDate compare: date1] == NSOrderedDescending) && ([userDate compare: date2] == NSOrderedAscending)){
//do something
}