简体   繁体   English

如何让 dateTimePicker 不依赖于系统日期格式 C#

[英]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.我有这段代码可以在dateTimePicker上设置时间,但它在我的伙伴计算机上崩溃了,因为他的 Windows 系统日期格式是 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?如何强制dateTimePicker使用某种格式?

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

That must work according to msdn .这必须根据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";
        }

在此处输入图片说明

Edit after comment评论后编辑

Add this 2 lines in the upper method:在上面的方法中添加这 2 行:

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(...".如果这仍然不起作用,请尝试调试来自“new Datetime(...”中的参数的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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