DateTimePicker以错误格式显示日期

问题描述:

我有一个ListView,其中输入ID和日期。日期格式为dd/MM/yyyy。DateTimePicker以错误格式显示日期

enter image description here

在的SelectedIndexChanged物业我写了这个

If lvRecruitmentList.SelectedItems.Count > 0 Then 
    txtID.Text = lvRecruitmentList.SelectedItems(0).Text 
    dtpRecievedOn.Text = lvRecruitmentList.SelectedItems(0).SubItems(1).Text 
End If 

在dtpRecievedOn的DateTimePicker,日期显示这样

enter image description here

我已经设置了以下属性通过设计师

dtpRecievedOn.Format = Custom 
dtpRecievedOn.CustomFormat = "dd/MM/yyyy" 

编辑1

注意,在ListView日期格式为DD/MM/YYYY 并在dtpRecievedOn日期是在格式MM/DD/YYYY。

我怎样才能使格式为dd dtpRecievedOn显示日期/ MM/YYYY

+1

对不起,但我不明白您的问题?什么是错的,你期望的是什么? – 2011-05-03 10:53:16

+0

请参阅我的编辑 – 2011-05-03 10:56:52

Dim str As String() = "2/1/2000".Split("/"C) 
Dim dt As New DateTime(Convert.ToInt16(str(2)), Convert.ToInt16(str(1)), Convert.ToInt16(str(0))) 
dateTimePicker1.Value = dt.Date 
+1

'DateTime.ParseExact'可能是一个更简单的解决方案。 – Groo 2011-05-03 12:15:20

+0

据我推测原因是 - 数据的格式为dd/MM/yyyy。并通过做解析将交换日和月。如有任何问题,请纠正我。 – Pankaj 2011-05-03 12:28:17

+0

检查[msergeant](http://*.com/questions/5868354/datetimepicker-displays-date-in-wrong-format/5870711#5870711)的答案,有一个'ParseExact'的例子(实际上,'' TryParseExact',但它以同样的方式工作)。 'DateTime.Parse'和'DateTime.ParseExact'之间有区别。 – Groo 2011-05-03 17:15:23

你想设置的DateTimePicker的价值,而不是文本。您可以使用DateTime.ParseExactDateTime.TryParseExact将您的文本日期更改为DateTime。将您的第三行替换为:

Dim selectedDate As DateTime 
Dim format As String = "dd/MM/yyyy" 
Dim txt As String = lvRecruitmentList.SelectedItems(0).SubItems(1).Text 
selectedDate = DateTime.ParseExact(txt, format, CultureInfo.InvariantCulture) 
dtpRecievedOn.Value = selectedDate 
+0

中查看我的意见。不起作用。字符串dateString,格式; dateString =“06/15/2008”; format =“d”; CultureInfo provider = CultureInfo.CurrentCulture; DateTime结果; if(DateTime.TryParseExact(dateString,“dd/MM/yyyy”,null,DateTimeStyles.None,out result)) {string} str =“”; } – Pankaj 2011-05-03 17:29:17

+0

正确,'CultureInfo.InvariantCulture'应该用来代替'null'或'CurrentCulture'。 – Groo 2011-05-03 17:40:38

+0

在tryexactparse函数中,它被设置为null。一个变量被宣布为文化,但没有被使用。 – Pankaj 2011-05-03 17:43:16