0

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?

MethodMan
  • 18,625
  • 6
  • 34
  • 52
Fiendcoder1
  • 119
  • 1
  • 1
  • 12

2 Answers2

2

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

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • I just want to get the time, if that's possible. – Fiendcoder1 Nov 25 '16 at 00:20
  • @Fiendcoder1 not entirely sure I understand - if you're not interested in dates but only time, you might want to grab the `TimeOfDay` property and store that in a `TimeSpan[]` instead – Mathias R. Jessen Nov 25 '16 at 00:22
  • Yes, because I already have a date in a different column of my winform and a departure and arrival column. I have a formula if the user choose 11:59:99 before and 12:00:00 after. So I want just to get the time. – Fiendcoder1 Nov 25 '16 at 00:25
  • Hello. I've followed the top answer in this http://stackoverflow.com/questions/8337625/convert-am-pm-time-to-24-hours-format. And I get what exactly what I want, but how can I just store it as `HH:mm`? – Fiendcoder1 Nov 25 '16 at 00:36
  • 1
    `ToString("HH:mm")` – Mathias R. Jessen Nov 25 '16 at 00:37
  • I believe I can't do computation to `ToString("HH:mm")` – Fiendcoder1 Nov 25 '16 at 00:38
  • @Fiendcoder1 No, it will result in a string. You can't have both - either keep it as a `DateTime` and specify [column formatting](https://msdn.microsoft.com/en-us/library/f9x2790s(v=vs.110).aspx) to display `HH:mm`, or convert it to a string at display it directly – Mathias R. Jessen Nov 25 '16 at 00:41
  • Ok, I'll come back to you later. – Fiendcoder1 Nov 25 '16 at 00:44
  • I now understand. I really thought the if you enclose it to `""` it will be a `string`. But after doing some `if else` conditioning. it retains it type of `DateTime`. Thanks man – Fiendcoder1 Nov 25 '16 at 01:22
  • Well answered bro :) – Joshua Dela Cruz Nov 25 '16 at 01:30
0

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?

Community
  • 1
  • 1