0

So a DateTime picker control in C# accepts a DateTime object. Now I only need the time portion of it, so what I'm doing right now is creating a DateTime object with a 'dummy' date and the correct time, and just use the time value.

Is there a better way to do this? There's no constructor of DateTime that I know of.

EDIT: I've seen this already C# make datetimepicker work as only timepicker

The problem is that I want to assign a value programatically, not make the picker work as timepicker only.

What I'm doing now to set it to 00:10 is:

TimePicker.Value = DateTime.Today + new TimeSpan(00, 10, 0);
// or
TimePicker.Value = new DateTime(2000,1,1,0,10,0);

Just want to know if there's a cleaner way.

Gaspa79
  • 5,488
  • 4
  • 40
  • 63
  • I've seen that already. I've done that. The problem I'm having is that I want to programatically assign a time and I'm doing this stupid thing where I add a timespan to datetime minvalue and there has to be a better way – Gaspa79 Apr 08 '16 at 13:47
  • Sorry about my comment. I think I better mark the question. – bluetoothfx Apr 08 '16 at 13:48
  • @Damieh -- Your question is not clear at all. If your stating it's not a dupe of that other question, then I really don't know what you're asking. – rory.ap Apr 08 '16 at 13:49
  • What comment? Don't worry dude everyone's trying to help here. No need to apologize here (even if you're Canadian like me =P) – Gaspa79 Apr 08 '16 at 13:49
  • 1
    `new DateTime(dummyYear, dummyMonth, dummyDay, actualHour, actualMinute, actualSecond)`? It would help if you could show what you are currently doing. – juharr Apr 08 '16 at 13:50
  • Yes juharr! That's what I'm doing right now and I wanted to know if there was a better way. Apparently there's none =( – Gaspa79 Apr 08 '16 at 13:51
  • Why not use `DateTime.Today` then add the time part? – ChrisF Apr 08 '16 at 13:51
  • 1
    @ThariqNugrohotomo That's not a dupe since that's about getting the time from a `DateTime`, not creating a `DateTime` based on a specific time. – juharr Apr 08 '16 at 13:52
  • I did that also, you can add a TimeSpan to datetime mintime too. I just wanted to know if there was a better way. Thanks everyone for the help! – Gaspa79 Apr 08 '16 at 13:53
  • @Damieh Yeah, your only real options are creating the `DateTime` with the dummy date info or adding the time to some existing `DateTime` with the time portion zeroed out. Unless you did a `DateTime.ParseExact` of a time only string, but IMHO that's not cleaner. – juharr Apr 08 '16 at 13:54

1 Answers1

-2

Is this on a WinForm? If you set ShowUpDown to True, then the calendar drop down get replaced with an up down spinner. If you don't want the user to be able to interact with the control at all, just set the controls Enabled property to False

GreatJobBob
  • 271
  • 1
  • 8