I have three text field in which i want to show the current time. In the first text field,it shows only hours , in second text field it shows only minute and in last one it shows am or pm. For getting current time I use this code but how do I show according to my requirement?
code...
- (void)viewDidLoad {
NSDate* date = [NSDate date];
//Create the dateformatter object
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
//Set the required date format
[formatter setDateFormat:#"HH:MM"];
//Get the string date
NSString* str = [formatter stringFromDate:date];
//Display on the console
NSLog(#"%#",str);
//Set in the lable
time_label.text=[NSString stringWithFormat:#"%# AM",str];
[super viewDidLoad];
}
Thanks in advance...
NSDate* date = [NSDate date];
//Create the dateformatter object
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
//Set the required date format
[formatter setDateFormat:#"hh MM a"];
//Get the string date
NSString* str = [formatter stringFromDate:date];
//Display on the console
NSLog(#"%#",str);
NSMutableArray *Arr=[str componentsSeparatedByString:#" "];
textFeild1.text=[Arr objectAtIndex:0]; //hoursTextFeild
textFeild2.text=[Arr objectAtIndex:1]; //minutesTextFeild
textFeild3.text=[Arr objectAtIndex:2];//Am/Pm TextFeild
- (void)viewDidLoad {
NSDate* date = [NSDate date];
//Create the dateformatter object
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
//Set the required date format
[formatter setDateFormat:#"HH:MM"];
//Get the string date
NSString* str = [formatter stringFromDate:date];
//Display on the console
NSLog(#"%#",str);
//Set in the label
//seperate its components
NSMutableArray *array = [str componentsSeperatedByString:#" "];
[firstTextField setText:[array objectAtIndex:0]];
[secondTextField setText:[array objectAtIndex:1]];
[thirdTextField setText:#"AM/PM"];
[super viewDidLoad];
}
Related
I take date into string. date format is like this and string is
datenotification=20-02-2012
Now I want to change this into
20-feb-2012
but for checking purposes I just place that into a dateformatter and print that but it shows me an incorrect date
NSDateFormatter* currentdateformate = [[NSDateFormatter alloc] init];
[currentdateformate setDateFormat:#"dd-MM-YYYY"];
NSDate *d=[currentdateformate dateFromString:dateNotification];
NSString *str3=[currentdateformate stringFromDate:d];
[currentdateformate release];
Hey Check this example
NSString *string = #"12-02-2000";
NSDateFormatter* currentdateformate = [[NSDateFormatter alloc] init];
[currentdateformate setDateFormat:#"dd-MM-yyyy"];
NSDate *d=[currentdateformate dateFromString:string];
[currentdateformate setDateFormat:#"dd-MMM-yyyy"];
NSString *str3=[currentdateformate stringFromDate:d];
[currentdateformate release];
NSLog(#"S %#",str3);
output S 12-feb-2012
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);
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 have a unique requirement. In my app I need to get only the month from picker. Since Date picker cannot implement this, I made the custom picker listing only months. The required format of the date after the customer selects a month is 1-Jan-2011 to 1-feb-2011
Basically, the month is the only thing that is coming from uipickerview and the day and year are static.
Can someone help me out? Some code I have tried.
NSString *dateString = [[NSString alloc ]initWithFormat:#"2011-%d-1 GMT+05:30", monthFromPickerAsInteger];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:#"yy-MM-DD z4"];
NSDate *dateFromString = [[NSDate alloc] init];
// ta-daaa!
dateFromString = [dateFormatter dateFromString:dateString];
NSLog(#"%#", dateFromString);
I guess you want to do this,
NSString *dateString = [NSString stringWithFormat:#"2011-%d-1", monthFromPickerAsInteger];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-M-d"];
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter setDateFormat:#"d-MMM-yyyy"];
NSString *requiredDateString = [dateFormatter stringFromDate:dateFromString];
[dateFormatter release];
NSLog(#"%#", requiredDateString);
I trying to find a way to convert a string into a date with a given locale identifier.
I have for example an italian date (locale: it_IT) I want to convert into a valid date object.
NSDate *GLDateWithString(NSString *string, NSString *localeIdentifier) {
[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:localeIdentifier];
[formatter setLocale:locale];
[locale release];
NSDate *date = [formatter dateFromString:string];
[formatter release];
return date;
}
this code does not work, the date is nil.
I can't figure out how I should use the locale setting for my purpose.
The solution is to use getObjectValue:forString:range:error: method of NSDateFormatter and set the correct date and time style ad follow:
- (NSDate *)dateWithString:(NSString *)string locale:(NSString *)localeIdentifier {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeStyle:NSDateFormatterNoStyle];
[formatter setDateStyle:NSDateFormatterShortStyle];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:localeIdentifier];
[formatter setLocale:locale];
[locale release];
NSDate *date = nil;
NSRange range = NSMakeRange(0, [string length]);
NSError *error = nil;
BOOL converted = NO;
converted = [formatter getObjectValue:&date forString:string range:&range error:&error];
[formatter release];
return converted? date : nil;
}
example:
NSString *italianDate = #"30/10/2010";
NSString *italianLocale = #"it_IT";
NSDate *date = [myCustomFormatter dateWithString:italianDate locale:italianLocale];
I struggled with German formats as well sometime ago and solved the problem by supplying my own formatting string:
[formatter setDateFormat:#"dd.MMM.yyyy HH:mm:ss"];
Then:
[dateFormatter dateFromString:#"01.Dez.2010 15:03:00"];
will get a correct NSDate.