开发者

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.

DateTimePicker displays date in wrong format

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

DateTimePicker displays date in wrong format

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜