简体   繁体   English

DateTimePicker搞砸了格式c#

[英]DateTimePicker screws up the format c#

Alright, so I have 2 DateTimePickers with custom formats that are set when the form loads, 好吧,所以我有2个DateTimePickers,其中包含在表单加载时设置的自定义格式,

start.Format = DateTimePickerFormat.Custom;
end.Format = DateTimePickerFormat.Custom;
start.CustomFormat = "dd/MM/yyyy";
end.CustomFormat = "dd/MM/yyyy";

And, my code is supposed to get all weeks between these dates, which it does correctly, then print every week. 并且,我的代码应该在这些日期之间获得所有周,它正确地执行,然后每周打印。 I'm managing to do it this way: 我设法这样做:

DateTimePicker tempdt = start;
tempdt.Format = DateTimePickerFormat.Custom;
tempdt.CustomFormat="dd/MM/yyyy";

int a = getWeeks();//method that gets the weeks between start and end
int d = 0;

for (int i = 0; i < a; i++)
{
    d += 7; 
    MessageBox.Show(tempdt.Value.Date.AddDays(d).ToShortDateString());
}

The code works perfectly, and it does get the weeks precisely, however tempdt still seems to have a "mm/dd/yyyy" format. 代码工作得很完美,它确实准确地得到了几周,但是看起来似乎还有一个“mm / dd / yyyy”格式。

Any idea what I might be missing? 知道我可能会缺少什么吗?

The DateTimePickers have nothing to do with this. DateTimePickers与此无关。 Your issue is you call ToShortDateString() which for your current culture is set to display as mm/dd/yyyy . 您的问题是调用ToShortDateString() ,将当前文化设置为mm/dd/yyyy

Just use your custom format in the text box too. 只需在文本框中使用自定义格式。

MessageBox.Show(tempdt.Value.Date.AddDays(d).ToString("dd/MM/yyyy"));

Also from the code you have shown tempdt is completely unnecessary and it is never even shown to the user. 同样来自您显示的代码tempdt是完全没必要的,它甚至永远不会显示给用户。 You can also remove the Date call as you don't display the time anyway to the user and are only adding whole number days to the date. 您也可以删除Date呼叫,因为您不向用户显示时间,并且只将整数天添加到日期。 This lets you simplify your code to 这使您可以简化代码

int a = getWeeks();//method that gets the weeks between start and end
int d = 0;

for (int i = 0; i < a; i++)
{
    d += 7; 
    MessageBox.Show(start.Value.AddDays(d).ToString("dd/MM/yyyy"));
}

The point of the format of a DateTimePicker is to control what the user sees in the control. DateTimePicker格式的要点是控制用户在控件中看到的内容。 That 'tempdt' is not even being shown to the user at all so should not be being used at all. 那个'tempdt'甚至根本没有向用户显示,所以根本不应该被使用。 Your calculations are using the Value property of that DTP. 您的计算使用该DTP的Value属性。 That property is a DateTime and is completely unaware and unaffected by the format of the DTP. 该属性是DateTime,完全不知道并且不受DTP格式的影响。 Get rid of the DTP in the calculation and just use a DateTime. 在计算中摆脱DTP并使用DateTime。

When you display the results of the calculation, you have to convert that DateTime to a String in the appropriate format at that time. 显示计算结果时,必须以适当的格式将该DateTime转换为String。 If you want to display "dd/MM/yyyy" format then call .ToString("dd/MM/yyyy") rather than .ToShortDateString() because the latter will use the default system format. 如果要显示“dd / MM / yyyy”格式,请调用.ToString(“dd / MM / yyyy”)而不是.ToShortDateString(),因为后者将使用默认的系统格式。

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

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