Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
DateTimePicker搞乱了格式c#_C#_.net_Datetimepicker - Fatal编程技术网

DateTimePicker搞乱了格式c#

DateTimePicker搞乱了格式c#,c#,.net,datetimepicker,C#,.net,Datetimepicker,好的,我有两个DateTimePicker,它们的自定义格式在表单加载时设置 start.Format = DateTimePickerFormat.Custom; end.Format = DateTimePickerFormat.Custom; start.CustomFormat = "dd/MM/yyyy"; end.CustomFormat = "dd/MM/yyyy"; 而且,我的代码应该得到这些日期之间的所有星期,它做得正确,然后每周打印。 我正设法这样做: DateTimePi

好的,我有两个DateTimePicker,它们的自定义格式在表单加载时设置

start.Format = DateTimePickerFormat.Custom;
end.Format = DateTimePickerFormat.Custom;
start.CustomFormat = "dd/MM/yyyy";
end.CustomFormat = "dd/MM/yyyy";
而且,我的代码应该得到这些日期之间的所有星期,它做得正确,然后每周打印。 我正设法这样做:

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());
}
DateTimePicker tempdt=start;
tempdt.Format=DateTimePickPerformat.Custom;
tempdt.CustomFormat=“dd/MM/yyyy”;
int a=getWeeks()//方法,该方法获取开始和结束之间的周数
int d=0;
for(int i=0;i
这段代码工作得很好,它确实精确地获得了周数,但是tempdt似乎仍然有一个“mm/dd/yyyy”格式


知道我可能遗漏了什么吗?

日期计时器选择器与此无关。您的问题是调用
ToSortDateString()
,对于当前区域性,它被设置为显示为
mm/dd/yyyy

只需在文本框中使用自定义格式即可

MessageBox.Show(tempdt.Value.Date.AddDays(d).ToString("dd/MM/yyyy"));
另外,从您显示的代码来看,tempdt完全没有必要,它甚至从未向用户显示过。您也可以删除
Date
调用,因为您不会向用户显示时间,并且只向日期添加整数天。这使您可以将代码简化为

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"));
}
inta=getWeeks()//方法,该方法获取开始和结束之间的周数
int d=0;
for(int i=0;i
日期时间选择器的格式要点是控制用户在控件中看到的内容。“tempdt”根本没有显示给用户,因此根本不应该使用它。您的计算正在使用该DTP的Value属性。该属性是DateTime,完全不知道并且不受DTP格式的影响。在计算中去掉DTP,只使用DateTime


当显示计算结果时,必须将该日期时间转换为相应格式的字符串。如果要显示“dd/MM/yyyy”格式,请调用.ToString(“dd/MM/yyyy”)而不是.toSortDateString(),因为后者将使用默认的系统格式。

如果我让问题看起来很混乱的话。getWeeks();是一个整数方法,它获取DateTimePicker开始和DateTimePicker结束之间的周数。welp,这将修复它。我对DateTimePicker控件不太友好,而且我真的负担不起对“start”控件进行更改,因此如果AddDays()对其进行了更改,我想我宁愿将start解析为一个新的DTP控件。无论如何,感谢您的努力。我有点装傻,但是哦,好吧。AddDays不会(也不能,因为DateTime是一个不可变的结构)修改原始值。这就像调用
String.Replace一样(
它不能修改原始字符串,它会给你一个新字符串,你可以随心所欲地使用它。事实上,你编写代码的方式,你只是修改了
start.Format
start.CustomFormat
的值。当你对
tempdt
进行两次调用时,你很幸运地将它们修改为你想要的值勒迪已经让他们准备好了。