passed object nil into my second view - iphone

I have a CalendarView and I've passed the object to another UIView with a tableView in it.
In my CalendarView:
anotherView *anotherViewController = [[anotherView alloc] init];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd-MM-yyyy"];
NSString *dateString = [formatter stringFromDate:aDateObject];
anotherViewController.eventDate = aDateObject;
anotherViewController.eventDateTitle = dateString;
[formatter release];
[anotherViewController release];
I don't know why my object its nil in anotherView...

As el.severo says in the comments, ensure you log every step. Also there are issues with locales, you need to set the correct locale GB and us regios have different interpretations of dates. E.g. for US:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd-MM-yyyy"];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setLocale:locale];
[locale release];
NSLog(#"date obj:%#", aDateObject);
NSString *dateString = [formatter stringFromDate:aDateObject];
NSLog(#"date str:%#", dateString);
anotherViewController.eventDate = aDateObject;
anotherViewController.eventDateTitle = dateString;

Related

Datetime to date only in objective c

i have a date time like this
2012-01-01 10:00:00
i need to convert it into date only like this
2012-01-01
Can anyone help me please
this is my code
NSDate *adate = objIStruct_Conference.m_DtDate;
NSLog(#"DATE========%#",objIStruct_Conference.m_DtDate);
NSString *DateString = [NSString stringWithFormat:#"%#", adate ];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease]];
[formatter setDateFormat:#"yyyy:MM:dd"];
NSLog(#"NEW DATE====%#", [formatter stringFromDate:adate]);
[formatter release];
i am getting DATE===== 2012-01-01 10:00:00 , NEW DATE=====(null)
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy:MM:dd"];
NSLog(#"%#", [formatter stringFromDate:[NSDate date]]);
[formatter release];
See the Class Reference of NSDateFormatter
Please use this code:
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSLog(#"%#",[dateFormat stringFromDate:date]);
it seems your adate pointer would be nil, because it is working perfectly for me on the real device with a valid NSDate but when I set the _date to nil, the output will be nil as in your case.
NSDate *_date = [NSDate date];
NSDateFormatter *_dateFormatter = [[NSDateFormatter alloc] init];
[_dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
[_dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSLog(#"%#", [_dateFormatter stringFromDate:_date]);
the output is:
2012-08-08

How to convert nsstring into nsdate in iphone

i want to convert an nsstring into nsdate .i have created a global variable where i saving the date picked from the datepicker and then converting this variable to nsdate us the datefromstring function.But the string is not getting converted to date.What may be the problem.
TTimePickercontroller:
This is the class in which i have the code for the picker
-(NSString *)convertDueDateFormat{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setFormatterBehavior: NSDateFormatterBehavior10_4];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
dateString = [dateFormatter stringFromDate:datePicker.date];
return dateString;
//dateString is an nsstring variable that has been declared globally.
}
TAddAlarmController.
this is another class in which i want to convert the dateString varibale into nssdate
-(IBAction)save{
timepicker = [[TTimePickerController alloc]init];
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"hh:mm"];
d = [df dateFromString:dateString];
NSLog(#"%#", d);
//Here i am using the dateFromString function and converting the string variable dateString into nsdate variable d .in dateString variable value is getting passed but in d the value is not getting passed.
}
Please help me in solving the problem.Thanks
-(NSString *)convertDueDateFormat{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm"];
dateString = [dateFormatter stringFromDate:datePicker.date];
////some code
}
Try like this,
Try this code this will help you because I run this code successfully and return time.
UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];
pickerView.datePickerMode = UIDatePickerModeDate;
pickerView.hidden = NO;
pickerView.date = [NSDate date];
[self.view addSubview:pickerView];
NSString *dateString;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm"];
dateString = [dateFormatter stringFromDate:pickerView.date];
NSLog(#"String into Date:--->%#",dateString);

iphone: NSDateFormatter how to show time in 24 hours format?

NSDateFormatter *formatter1=[[NSDateFormatter alloc]init];
[formatter1 setDateFormat:#"HHmmss MMddyyyy"];
NSDate *finalDate =[formatter1 dateFromString:testTime];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[lblpickUpTime setText:[formatter stringFromDate:finalDate]];
[formatter release];
[formatter1 release];
where testTime = #"201518 07122011"; and I want the result in 24 hours time
e.g. 20.15 rather than 8:15 PM
and is there any better way to do this?
Try by setting
[formatter setDateFormat:#"HH:mm"]; // replacement for [formatter setTimeStyle:NSDateFormatterShortStyle];
Alternate method (without using any formatters):
NSRange range;
range.location = 2;
range.length = 2;
NSString *dateString = [NSString stringWithFormat:#"%#:%#",[myString substringToIndex:2],[myString substringWithRange:range]];
If you specify a format instead of using NSDateFormatterShortStyle, you can have the output in any format you want. For example:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd 'at' HH:mm"];
[lblpickUpTime setText:[formatter stringFromDate:finalDate]];
[formatter release];

unable to fetch NSDate from NSString

I am able to fetch NSString representing the current date and time in PST using the following code.
//get current date and time
NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:#"America/Los_Angeles"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yy HH:mm:ss zzz"];
[dateFormatter setTimeZone:timeZone];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setLocale:enUSPOSIXLocale];
[enUSPOSIXLocale release];
NSDate *date = [NSDate date];
NSString *currentDateStr = [dateFormatter stringFromDate:date];
NSLog(#"currentDateStr %#",currentDateStr);
[dateFormatter release];
EDIT:
But the following code to fetch NSDate from NSString doesn't serve my purpose.
//get date from string
NSDateFormatter* formatter2 = [[NSDateFormatter alloc] init];
[formatter2 setDateFormat:#"MM/dd/yy HH:mm:ss"];
NSDate *dt = [[NSDate alloc]init];
dt = [formatter2 dateFromString:currentDateStr];
NSLog(#"dt %#",dt);
"dt" appears to be null.
Could anybody help me out in solving this issue of getting NSDate from NSString.
Thanx in advance.
NSDateFormatter also has dateFromString: function.
EDIT:
//get date from string.
NSDateFormatter* formatter2 = [[NSDateFormatter alloc] init];
[formatter2 setDateFormat:#"MM/dd/yy HH:mm:ss zzz"];
NSDate *dt = [formatter2 dateFromString:currentDateStr];
NSLog(#"dt %#",dt);
Make use of
[dateFormatter dateFromString:urString];
In your case urString is currentDateStr
Use
- (NSString *)datePickerValueChangedd:(UIDatePicker*) datePicker{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 50, 68, 68)];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
label.text = [NSString stringWithFormat:#"%#",[df stringFromDate:datePicker.date]];
NSLog(#"I am inside the datePickerValueChanged - - %#", label.text);
[df release];
globalVariable= label.text;
return (label.text);
}

iPhone SDK Objective-C __DATE__ (compile date) can't be converted to an NSDate

//NSString *compileDate = [NSString stringWithFormat:#"%s", __DATE__];
NSString *compileDate = [NSString stringWithUTF8String:__DATE__];
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:#"MMM d yyyy"];
//[df setDateFormat:#"MMM dd yyyy"];
NSDate *aDate = [df dateFromString:compileDate];
Ok, I give up. Why would aDate sometimes return as nil?
Should it matter if I use the commented-out lines... or their matching replacement lines?
It can return nil if the phone's Region setting is not US (or equivalent).
Try setting the formatter's locale to en_US:
NSString *compileDate = [NSString stringWithUTF8String:__DATE__];
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:#"MMM d yyyy"];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[df setLocale:usLocale];
[usLocale release];
NSDate *aDate = [df dateFromString:compileDate];
Slightly modifying DyingCactus' answer for ARC enabled code (for easier copying-n-pasting):
NSString *compileDate = [NSString stringWithUTF8String:__DATE__];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MMM d yyyy"];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[df setLocale:usLocale];
NSDate *aDate = [df dateFromString:compileDate];