DateTimePicker displays date in wrong format
I have a ListView where ID and date is entered. The date is in the format dd/MM/yyyy.
In the Selected开发者_JAVA技巧IndexChanged Property I have written this
If lvRecruitmentList.SelectedItems.Count > 0 Then
txtID.Text = lvRecruitmentList.SelectedItems(0).Text
dtpRecievedOn.Text = lvRecruitmentList.SelectedItems(0).SubItems(1).Text
End If
In the dtpRecievedOn DateTimePicker, the Date is displayed like this
I have set the following properties via designer
dtpRecievedOn.Format = Custom
dtpRecievedOn.CustomFormat = "dd/MM/yyyy"
Edit 1 :
Notice that in the ListView the Date is in the format dd/MM/yyyy and in the dtpRecievedOn the Date is in the format MM/dd/yyyy.
How can I make dtpRecievedOn display date in the format dd/MM/yyyy
You'll want to set the Value of the DateTimePicker as opposed to the Text. You can use DateTime.ParseExact
or DateTime.TryParseExact
to change your text date to a DateTime. Replace your third line with:
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
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
精彩评论