I am having trouble getting the time for today from a string. I am reading in a time from a UITextField, but the date is coming out as the start of the epoch
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH-mm"];
[formatter setDateStyle:NSDateFormatterNoStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setLenient:YES];
NSLog(@"Format: %@",[formatter dateFormat]);
NSDate *now = [formatter dateFromString:[textbox text]];
NSLog(@"Time from textfield: %@",now]);
This gives the following date from the log code.
2011-01-19 18:56:28.193 MyApp[8284:207] The time for now: 1970-01-01 06:00:00 GMT
Setting the
dateStyleandtimeStylefor the formatter will override thedateFormatthat you have set manually. The following should work as expected:The resulting date object will be close to the start of the epoch because you are not providing a year, month and day. If you want the time in the textfield to represent a time on today’s date, you will have to add in some extra code: