How to get right time from Custom UIDatePicker in ios - iphone

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

Related

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

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

Wrong Date from string returns from formatter! Why?

I have problem with NSDateFormatter
I converting date from picker in one view
`NSDateFormatter *output = [[NSDateFormatter alloc] init];
[output setDateStyle:NSDateFormatterMediumStyle];
[output setDateStyle:NSDateFormatterShortStyle];
[output setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSString *StringToSend = [output stringFromDate:datePicker.date];
`
then send string to other nib
where converting it back with that code
`NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateStyle:NSDateFormatterMediumStyle];
[inputFormatter setDateStyle:NSDateFormatterShortStyle];
[inputFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate *formatterDate = [inputFormatter dateFromString:StringFromOtherView];
NSLog(#"%#", formatterDate);`
and it's return wrong date
sending 2011-09-26 01:02:49
geting 2011-09-25 22:02:49 +0000
what is wrong?
Because:
When you NSLog() an NSDate, it always logs it in GMT
You live in a timezone that's three hours ahead of of GMT. Thus, 1:02 am for you is 22:02 pm (of the previous day) in GMT.
Include the timezone on your date format string.
+ (NSDate*) dateFromString:(NSString*)aStr
{
if (aStr.length == 10) {
aStr = [aStr stringByAppendingFormat:#" 00:00:00"];
}
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM-dd-yyyy HH:mm:ss"];
dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
NSLog(#"strdate %#", aStr);
NSDate *aDate = [dateFormatter dateFromString:aStr];
NSLog(#"date = %#",aDate);
[dateFormatter release];
return aDate;
}

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

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

I want to convert NSString to NSDate but UIPickerView shows current date

I want to convert NSString to NSDate but UIPickerView shows current
datedatePicker=[[UIDatePicker alloc]init];
datePicker.userInteractionEnabled=YES;
datePicker.minuteInterval=5;
NSLog(#"Date");
////////
NSString *curDate=alarmData.date;
//Jan 14, 2011 11:37
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"MMM dd, yyyy HH:mm"];
//Optionally for time zone converstions
[format setTimeZone:[NSTimeZone timeZoneWithName:nil]];
NSDate *da = [format dateFromString:curDate];
[datePicker setDate:da animated:YES];
any suggestions
#Ali try this and u will get the string from date
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MMM dd, yyyy HH:mm"];
//conversion of NSString to NSDate
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [formatter dateFromString:curDate];
[formatter release];