简体   繁体   中英

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?

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

I am not able to comment becos I just started this account. This answer is related to a conversation in comments on how to change to HH:mm

DateTimePicker.ShowUpDown = true;
DateTimePicker.CustomFormat = "hh:mm";
DateTimePicker.Format = System.Windows.Forms.DateTimePickerFormat.Custom;

Credits to here: DateTime Picker In WinForm How To Pick Time?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM