How to change/set DateTimePicker value

你说的曾经没有我的故事 提交于 2019-12-12 10:38:41

问题


I am trying to set / change the DateTimePicker value (using vb.net) but can't work out how to do it.

I have added the control to the page and I have tried using the following code but it doesn't work and I can't work out how to set the value during run-time.

DateTimePicker1.Value = Now.Day & "-" & Now.Month & "-" & Now.Year

The format of the control is set to Long and it looks like this when first loaded:

Tuesday, February 26, 2013

But I can't work out how to change it.

The error I get based on my code above is:

Conversion from string "26-2-2013" to type 'Date' is not valid.

Anyone got any ideas ?


回答1:


I ended up getting it working by doing the following:

DateTimePicker1.Value = New Date(2013, 2, 26)

I was loading the value wrong.




回答2:


Add the following code on Form Load Event to set the date time picker value to today:

DateTimePicker1.CustomFormat="dd-MM-yyyy"
DateTimePicker1.Value=Now()


来源:https://stackoverflow.com/questions/15082664/how-to-change-set-datetimepicker-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!