1

Is there any view similar to NSDatePicker for OS X, but for selecting durations (i.e. hours, minutes and seconds) rather than entire dates. A Google search didn't bring anything useful up, and I haven't yet succeeded in customizing NSDatePicker to be suitable for durations only - my attempts don't work properly with 12-hour (AM and PM) date modes and different timezones.

MrMage
  • 7,282
  • 2
  • 41
  • 71
  • When you say durations, do you mean that you want, say 10:30:05, to mean 10 hours 30 minutes and 5 seconds rather than the time 10:30:05? What range of values do you want, for instance, do you want to be able to set the hours to double digits, triple digits? – rdelmar Apr 24 '12 at 15:39
  • Yes, that's what I mean with durations. One-digit hours would be sufficient. If you can provide an example to tweak `NSDatePicker` to behave like that (without glitches!), that would be sufficient. – MrMage Apr 24 '12 at 20:49
  • I found some problems with what I posted before, so I edited it again, and I think what I found works pretty well. – rdelmar Apr 26 '12 at 00:13

2 Answers2

1

I don't think there is any ready-made solution, but it shouldn't be hard to make one of your own with 3 borderless text fields and a couple of labels with colons in them, all inside an NSBox with a stepper next to it.

I think I found a way that works with an NSDate picker. I'm not sure why some of it works, but it seems necessary to set the locale to somewhere that uses a 24 hour clock, like Japan and set a calendar for the picker. The only "glitch" I see, is that there is a bit of a delay when typing in some 1 digit numbers (this is something Apple changed in OS X 10.5 I think to have the editor wait to see if you want to add a second digit). However, you can get it to update immediately by tabbing out of the field or clicking in another field. So here's the setup:

self.picker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"ja_JP"];
    self.cal = [NSCalendar currentCalendar];
    self.cal.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
    self.picker.calendar = self.cal;
    self.picker.timeZone = cal.timeZone;

And here is the action method for the picker:

    -(IBAction)dpClick:(NSDatePicker *)sender {
    NSDateComponents *comps = [cal components:NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:sender.objectValue];
    NSLog(@"hours=%ld  minutes=%ld  seconds=%ld",comps.hour,comps.minute,comps.second);
}
rdelmar
  • 103,982
  • 12
  • 207
  • 218
0

You can use the -[NSDatePicker setDatePickerElements:] method to configure the date picker to only show the time portion of a date.

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498