Fiendcoder1 Fiendcoder1 - 13 days ago 6
C# Question

Storing DateTime into Array in c#

I have initialized an array for my

DateTime
.

DateTime[] departureCalcArray = new DateTime[10];


And then getting the value of the
DateTimePicker
that is formatted as
h:mm tt
(that is 8:30 AM with no preceeding zero) .

My code for storing the value of the
DateTimePicker
is as below.

departureCalcArray[i] = timeDeparture.Value.Date;


However, when I checked if the value is saved via
MessageBox.Show();

I keep getting the date today and 12:00:00 AM. Although back in PHP, I use to convert the time to 24hour format so that I can use it in calculation. Any help please?

Answer

Change

departureCalcArray[i] = timeDeparture.Value.Date;

to

departureCalcArray[i] = timeDeparture.Value;

When you use the Date property of a DateTime instance, you get a new DateTime instance with the same date, but its Time component set to 12:00AM