strange date from uidatepicker - iphone

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

Related

Retrieving hours in 24 hour format in iphone

I am using this format to get hours
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"hh";
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSString *date = [dateFormatter stringFromDate:now];
NSInteger myHour = [date intValue];
The problem is it returns e.g 4Pm i Want it to return 16 Instead of 4. How can I do that? I tried replacing hh by single h by of no avail.
Here Your input:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh"];
NSDate *Yourcurrenttime = [dateFormatter dateFromString:Yourstring];
And date converted to the String ..
[dateFormatter setDateFormat:#"HH"];
NSString *date = [dateFormatter stringFromDate:now];
NSInteger myHour = [date intValue];
Here is answer :-
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"HH";
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSString *date = [dateFormatter stringFromDate:[NSDate date]];
NSInteger myHour = [date intValue];
NSLog(#"%d",myHour);
Hope it helps you!
Its for 12 hour time format :-
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:#"hh a"];
NSString *theTime = [timeFormat stringFromDate:[NSDate date]];
NSLog(#"time, %#",theTime);
Its for 24 hour time format:-
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:#"HH"];
NSString *theTime = [timeFormat stringFromDate:[NSDate date]];
NSLog(#"time, %#",theTime);

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

ios: String to Date conversion

I am trying to convert my date from NSString to NSDate using following code
NSDateFormatter *dateformatter = [[NSDateFormatter alloc]init];
[dateformatter setDateStyle:NSDateFormatterShortStyle];
[dateformatter setTimeStyle:NSDateFormatterNoStyle];
[dateformatter setDateFormat:#"dd/MM/yyyy"];
NSDate *myDate = [[NSDate alloc] init];
currMonth = 3;
currYear = 2012;
NSString *str = [NSString stringWithFormat:#"01/%2d/%d", currMonth, currYear];
str = [str stringByReplacingOccurrencesOfString:#" " withString:#"0"];
myDate = [dateformatter dateFromString:str];
NSLog(#"myStr: %#",str);
NSLog(#"myMonth: %2d",currMonth);
NSLog(#"myYear: %d",currYear);
NSLog(#"myDate: %#",myDate);
Above code is giving me wrong date. Can anyone please help?
What is your output? Keep in mind, that NSDate is in UTC.
2012-03-01 00:00:00 (GMT+1) is 2012-02-39 23:00:00 (UTC)
Another tip:
%02 formats your integers with leading zeros.
Try this:
-(NSDate *)dateFromString:(NSString *)string
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormat setLocale:locale];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSTimeInterval interval = 5 * 60 * 60;
NSDate *date1 = [dateFormat dateFromString:string];
date1 = [date1 dateByAddingTimeInterval:interval];
if(!date1) date1= [NSDate date];
[dateFormat release];
[locale release];
return date1;
}
Tell me if it helps u :]
try
NSDateFormatter *dateformatter = [[NSDateFormatter alloc]init];
[dateformatter setDateStyle:NSDateFormatterShortStyle];
[dateformatter setTimeStyle:NSDateFormatterNoStyle];
[dateformatter setDateFormat:#"dd/MM/yyyy"];
NSDate *myDate = [[NSDate alloc] init];
int currMonth = 3;
int currYear = 2012;
NSString *str = [NSString stringWithFormat:#"01/%2d/%d", currMonth, currYear];
str = [str stringByReplacingOccurrencesOfString:#" " withString:#"0"];
//SET YOUT TIMEZONE HERE
dateformatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"PDT"];
myDate = [dateformatter dateFromString:str];
NSLog(#"myStr: %#",str);
NSLog(#"myMonth: %2d",currMonth);
NSLog(#"myYear: %d",currYear);
NSLog(#"myDate: %#",myDate);

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

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