Setting datePicker (That is set to time mode) with a string - iphone

I have a string in this form : "h:mm a" and im trying to set a date picker to that time. I have tried this :
NSDate *exampleDateFromString = [dateFormat dateFromString:#"8:48 AM"];
datepicker.date = exampleDateFromString;
But it didn't work, is this because you are not able to set a date from a string? Or am I doing it wrong? If so, how do I do it right?
Edit
I tried this too:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"h:mm a"];
NSDate *exampleDateFromString = #"8:48 AM"];
datepicker.date = exampleDateFromString;
My date picker is
datepicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
datepicker.datePickerMode = UIDatePickerModeDate;
datepicker.minuteInterval = 5;
datepicker.datePickerMode = UIDatePickerModeTime;
[datepicker addTarget:self action:#selector(picker1Changed) forControlEvents:UIControlEventValueChanged];
Edit
The problem was setting the date before I allocated and initiated the picker. Sorry all for the trouble.

This should work:
NSDateFormatter *dateFormat;
dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"h:mm a"];
NSDate *exampleDateFromString =[dateFormat dateFromString:#"8:48 AM"];
[datepicker setDate:exampleDateFromString]
If this does not work it means that you have not set datepicker as an oultet.

Well, first to clarify:
NSDate is a Objective-C class for holding date, and
NSDateFormatter is a Objective-C class for setting and changing date format
So what you want to do is this:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"h:mm a"];
NSDate *exampleDateFromString = [dateFormatter dateFromString:#"8:48 AM"];
datepicker.date = exampleDateFromString;
This should do the trick.
The point is that you set your data format in NSDateFormatter, and use its methods to return NSDate from data string (i.e 8:48 AM) in the format you've described.

You should set a well-defined locale, otherwise the format "h:mm a" might be interpreted
in some locale-dependent way:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"h:mm a"];
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
NSDate *exampleDateFromString = [dateFormat dateFromString:#"8:48 AM"];
datepicker.date = exampleDateFromString;

Try THis...it will work
NSDateFormatter *dateFormat;
dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"h:mm a"];
NSDate *exampleDateFromString =[dateFormat dateFromString:#"8:48 AM"];
date-picker.minimumDate=exampleDateFromString;
[date-picker setDate:exampleDateFromString];

Related

How to get right time from Custom UIDatePicker in ios

Hello I'm Beginner in ios I Have create Custom UIDatePicker View This show right date and time but when we click on done button then this show date right but time is not right ....So how do this ....
UIDatePicker *DatePicker;
UITexField *DateTime;
if ([[UIScreen mainScreen] bounds].size.height >480)
{
DatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,350,320,50)];
}
else
{
DatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,270,320,50)];
}
DatePicker.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleTopMargin;
DatePicker.datePickerMode = UIDatePickerModeDateAndTime;
DatePicker.date = [NSDate date];
[self.DateTime setInputView:DatePicker];
DatePicker.userInteractionEnabled=YES;
[self.view addSubview:DatePicker];
UIDatePicker *picker = (UIDatePicker*)self.DateTime.inputView;
self.DateTime.text = [NSString stringWithFormat:#"%#",picker.date];
NSLog(#"date picker is %#",self.DateTime.text);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss +HHmm"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSDate *myDate = [dateFormatter dateFromString:[NSString stringWithFormat:#"%#",DateTime.text]];
NSLog(#"%#", [dateFormatter stringFromDate:myDate]);
NSDateFormatter *anotherDateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setAMSymbol:#"AM"];
[dateFormatter setPMSymbol:#"PM"];
[anotherDateFormatter setDateFormat:#"dd/MM/yyyy hh:mm a"];
NSLog(#"%#", [anotherDateFormatter stringFromDate:myDate]);
DateTime.text =[anotherDateFormatter stringFromDate:myDate];
NSLog(#"datetime is %#",DateTime.text);
this picker showh right time and date in UI but when we click done button(not mention here)then it show .... DateTime.text=date picker is 2013-07-03 05:22:57 +0000 after this format it shows 2013-07-03 12:00:57 +0000
03/07/2013 12:00 AM
datetime is 03/07/2013 12:00 AM
When we Select any time then this show only above time ....Date is right ...but time is wrong so ....solve this problem
this code may be help you
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
//[formatter setTimeZone:[NSTimeZone systemTimeZone]];
[formatter setDateFormat:#"dd-MM-yyyy hh:mm"];
strDatetime = [formatter stringFromDate:picker.date];
[datetimeLbl setText:strDatetime];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"HH:mm"]; //24hr time format
NSString *dateString = [outputFormatter stringFromDate: picker.date];
NSLog(#"%#",dateString);
It will print the proper time.
This may be an issue of NSTimeZone.
You can try this
NSTimeInterval seconds; // assume this exists
NSDate* ts_utc = [NSDate dateWithTimeIntervalSince1970:seconds];
NSDateFormatter* df_utc = [[[NSDateFormatter alloc] init] autorelease];
[df_utc setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[df_utc setDateFormat:#"yyyy.MM.dd G 'at' HH:mm:ss zzz"];
NSDateFormatter* df_local = [[[NSDateFormatter alloc] init] autorelease];
[df_local setTimeZone:[NSTimeZone timeZoneWithName:#"EST"]];
[df_local setDateFormat:#"yyyy.MM.dd G 'at' HH:mm:ss zzz"];
NSString* ts_utc_string = [df_utc stringFromDate:ts_utc];
NSString* ts_local_string = [df_local stringFromDate:ts_utc];

How to format data from string variable

I have a NSString that keeps date:
1900-01-01T11:00:00
I need to format it, so I have a formatter:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"dd/MM/yyyy hh:mm:ss a"];
How to format this string now?
NSString* formatedDateString = [df ???];
Create a date formatter, that returns a date object for for the given string, create a second date formatter, that returns a string in the desired format from that date.
NSDateformatter *inputFormatter = [[NSDateformatter alloc] init];
[inputFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
[inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
NSDate *date = [inputFormatter dateFromString:dateString];
NSDateformatter *outputFormatter = [[NSDateformatter alloc] init];
[outputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
[outputFormatter setDateFormat:#"dd/MM/yyyy hh:mm:ss a"];
NSString *outputString = [outputFormatter stringFromDate:date];
But you could also let the output date formatter decide the style in respect to the locale:
NSDateformatter *outputFormatter = [[NSDateformatter alloc] init];
[outputFormatter setDateStyle:NSDateFormatterShortStyle];
[outputFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *outputString = [outputFormatter stringFromDate:date];
for a american locale, you should now cat the desired format.
if you want to enforce format "dd/MM/yyyy hh:mm:ss a" for the user, you should also set the locale of the output date formatter.
a complete command line example.
int main(int argc, const char * argv[])
{
#autoreleasepool {
NSString *dateString1 = #"2012-12-31T11:00:00";
NSString *dateString2 = #"2012-12-31T14:00:00";
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
[inputFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
[outputFormatter setDateFormat:#"dd/MM/yyyy hh:mm:ss a"];
NSDate *date1 = [inputFormatter dateFromString:dateString1];
NSDate *date2 = [inputFormatter dateFromString:dateString2];
NSString *outputString1 = [outputFormatter stringFromDate:date1];
NSString *outputString2 = [outputFormatter stringFromDate:date2];
NSLog(#"%#", outputString1);
NSLog(#"%#", outputString2);
}
return 0;
}
If you dont set the locale like [outputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]]; it will lead do a ugly mix of formats with user's sefault locale, if it not happend to be 'en_US'
ie in German: 31/12/2012 02:00:00 nachm. This is a format not used in german.
To support user's language and locale you should instead use the different styles.
The q&a rmaddy provided indicates, that there are rare cases, where the parse format gets rewritten. To avoid this, set the input formatted's format to a certain locale, like [inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
You need to create two date formatter, one for parsing, one for formatting; Your parsing format is "yyyy-MM-ddThh:mm:ss" and your output format is "dd/MM/yyyy hh:mm:ss a". So, something like this :
NSString * targetString = #"1900-01-01'T'11:00:00";
NSDateFormatter *parseFormat = [[NSDateFormatter alloc] init];
[parseFormat setDateFormat:#"yyyy-MM-ddThh:mm:ss"];
NSDate * date = [parseFormat dateFromString:targetString];
NSDateFormatter * outputFormat = [[NSDateFormatter alloc] init];
[outputFormat setDateFormat:#"dd/MM/yyyy hh:mm:ss a";
NSString * outputString = [parseFormat stringFromDate:date];
example for getting current date :
-(void) getDate
{
NSString *theDate;
NSString *theTime;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:#"HH:mm:ss"];
NSDate *now = [[NSDate alloc] init];
theDate = [dateFormat stringFromDate:now];
theTime = [timeFormat stringFromDate:now];
//2012-09-21 02:00:00
self.deviceCurrentTime=[NSString stringWithFormat:#"%# %#",theDate,theTime];
}

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);

strange date from uidatepicker

I use date picker and I set its mode to be
pickerView.datePickerMode = UIDatePickerModeDate ;
when select august 3o, 2011
to display It I use
NSDate * selected = [pickerView date];
NSString * date = [selected description];
DateLabel.text = date;
the output is 2011-08-29 21:00:00:00 + 0000
any suggestion on how to display the correct date only
Have a look at the NSDateFormatter class.
try using:
NSDate *selected = [pickerView date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeStyle:kCFDateFormatterNoStyle];
[formatter setDateStyle: kCFDateFormatterShortStyle];
NSString *date = [formatter stringFromDate:selected];
[formatter release];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"DD/MM/yyyy"];//you can change format here #"MM/yyyy"
DateText.text = [dateFormatter stringFromDate:datePicker.date];
[dateFormatter release];

NSDateFormatter and yyyy-MM-dd

I'm trying to take a NSString date in the format "2/22/11" and convert it to this format: 2011-02-22
This is my code:
NSDate *dateTemp = [[NSDate alloc] init];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
dateTemp = [dateFormat dateFromString:newInvoice.date];
newInvoice.date = [dateFormat stringFromDate:dateTemp];
newInvoice.date starts as an NSString equal to "2/22/11". dateTemp ends up NIL. newInvoice.date ends up NIL as well.
I can't for the life of me figure out why.
You are facing this problem because your date formatter is not correct.Suppose your newInvoice.date variable store "11:02:23" the your dateFormatter should be #"yy:MM:dd" and if your newInvoice.date variable store"2/22/11" then your dateFormatter should be #"MM/dd/yy"
NSDate *dateTemp = [[NSDate alloc] init];
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
NSDateFormatter *dateFormat2 = [[NSDateFormatter alloc] init];
[dateFormat1 setDateFormat:#"MM/dd/yy"];
[dateFormat2 setDateFormat:#"yyyy-MM-dd"];
dateTemp = [dateFormat1 dateFromString:newInvoice.date];
newInvoice.date = [dateFormat2 stringFromDate:dateTemp];
both format should be according to your requirement to get the correct result
This should work fine. I have set Date style. You can change NSDateFormatterShortStyle to something else. Also check your newInvoice.date format.
NSDate *dateTemp = [[NSDate alloc] init];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateStyle: NSDateFormatterShortStyle];
dateTemp = [dateFormat dateFromString: newInvoice.date];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
newInvoice.date = [dateFormat stringFromDate:dateTemp];