Muthukamatchi Ganesan
Muthukamatchi Ganesan

Reputation: 373

How do I enter the time value in hh:mm:ss format?

I need to enter the time value hh:mm:ss (09:45:56) format , but in the Xcode text field I always get (094556) as format -
how to resolve this?

Upvotes: 2

Views: 1369

Answers (2)

Warren Burton
Warren Burton

Reputation: 17382

You can use NSDateFormatter to parse any custom format

NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];

dateformatter.dateFormat = @"HH':'mm':'ss";

NSString *mydate = @"09:45:56" 

NSDate *parseddate = [dateformatter dateFromString:mydate];

This should give you a date of "9.45 am today"

The Docs for NSDateFormatter are pretty good but you can find the specific formatter variants possible here. Its linked from the apple docs.

http://www.unicode.org/reports/tr35/tr35-19.html#Date_Format_Patterns

Upvotes: 2

mhunter007
mhunter007

Reputation: 114

If I am understanding the question correctly, you want to format the text in HH:mm:ss format as it is being typed in a UITextField?

In that case, make your view controller a UITextFieldDelegate and implement this selector:

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 

Every time the user types a character, or deletes it, this method will be called. At that point, you can determine whether to format the string. For instance, after they've typed the second character, thus creating the hour component of 09, you can append a ":" to the string so that it looks like "09:".

Another option. Use this selector to format it after they've finished typing in the entire string:

- (void)textFieldDidEndEditing:(UITextField *)textField

Upvotes: 1

Related Questions