Loplac92 Loplac92 - 1 year ago 104
C# Question

Combining integers into a DateTime

I have 3 integer values and I'm trying to combine them to create a datetime variable. I'm trying to do this as I'm needing the user to specify the year through a datetimepicker and then in an array I need the date to start at the first day of the first month of that year.

Currently I have,

int b = 1;
int m = 1;
int y = dateTimePicker1.Value.Year;

DateTime newdate = new DateTime(b, m, y);

I've tried a whole range of different ways of combing the integers together to form 1/1/2017. I know that the integers are holding the correct values when the error appears, but the newdate value is 01/01/0001 12:00:00:AM.

I don't know why the integer y is being changed from 2017 to 0001?

As a result the error message is, Year, Month and Day parameters describe an
un-representable DateTime.

Answer Source

First you need to change parameter order

 DateTime newdate = new DateTime(year, month, day);

Second you can use DateTime.Parse or DateTime.TryParse to get date object from string

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download