0

On a wpf application i have a DatePicker, and, a TimePicker ( from the Extended WPF Toolkit). These two contrôls return a DateTime Value.

I would like to store the result of this two control in a one DateTime variable.

I tried :

myNewEvenement.dDate = DateTimePicker_Date.SelectedDate.Value;
myNewEvenement.dDate += (DateTime)TimePicker_Heure.Value;

But i cannot compile this code.

Any idea please ?

Thanks a lot

Walter Fabio Simoni
  • 5,671
  • 15
  • 55
  • 80

3 Answers3

1

Use DateTime.Add(TimeSpan value) method to create new DateTime object (you can't change value of existing DateTime struct, because it is immutable):

myNewEvenement.dDate = DateTimePicker_Date.SelectedDate.Value.Value
                           .Add(TimePicker_Heure.Value.Value.TimeOfDay);

Or more readable:

DateTime date = DateTimePicker_Date.SelectedDate.Value.Value;
TimeSpan time = TimePicker_Heure.Value.Value.TimeOfDay;
myNewEvenement.dDate = date.Add(time);
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
1

The TimePicker's Value is a DateTime. Since you want to add the time to the date, simply add the TimeOfDay property of the time picker's value when adding, such as:

myNewEvenement.dDate = DateTimePicker_Date.SelectedDate.Value;
myNewEvenement.dDate += TimePicker_Heure.Value.TimeOfDay;
Fls'Zen
  • 4,594
  • 1
  • 29
  • 37
1

DateTime is a struct, hence it is immutable. You can create a new DateTime by using DateTime.Add with the appropriate TimeSpan:

long ticks = ((DateTime)TimePicker_Heure.Value).Ticks;
TimeSpan ts = TimeSpan.FromTicks(ticks);
myNewEvenement.dDate = myNewEvenement.dDate.Add(ts);

or directly via DateTime.AddTicks

myNewEvenement.dDate = myNewEvenement.dDate.AddTicks(ticks);
Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939