Pavilion Sahota
Pavilion Sahota

Reputation: 105

How do I get the dateTimePicker to not rely on system date format C#

I have this code that sets the time on the dateTimePicker but it crashes on my mates computer because his Windows system date format is dd/MM/yy. I tried to set a custom time in the property field and in code but it doesn't accept it. How do I force the dateTimePicker to use a certain format?

deadline_dtp.CustomFormat = "dd-MM-yyyy";
deadline_dtp.Format = DateTimePickerFormat.Custom;
deadline_dtp.Value = new DateTime(idYYYY, idMM, idDD);

Upvotes: 1

Views: 757

Answers (1)

ermir
ermir

Reputation: 877

That must work according to msdn.

Give it a try by setting the format before the custom format.

Example

private void Form1_Load(object sender, EventArgs e)
        {
            dateTimePicker1.Format = DateTimePickerFormat.Custom;
            dateTimePicker1.CustomFormat = "dd-MM-yyyy";
        }

enter image description here

Edit after comment

Add this 2 lines in the upper method:

var rightNow = DateTime.Now;
dateTimePicker1.Value = new DateTime(rightNow.Year, rightNow.Month, rightNow.Day);

If that is still not working, try to debug the values that are coming from your arguments in "new Datetime(...".

Upvotes: 1

Related Questions