Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting DatePicker Value

I currently have a program that takes the value from a datePicker and have the date saved as a string. I only needed the date not the time so i used the following code to save the date value:

DateTime StartDate;
String inMyString;
savedDate = datePicker1.SelectedDate.Value.Date;
inMyString =   savedDate.Date.ToShortDateString()

I have the inMyString pushedBack into my list and now i want to place it back into the datePicker.

On MSDN is shows me the following example to set the date.

dateTimePicker1.Value = new DateTime(2001, 10, 20);

the problem is that having .Value after my date picker is not an option (it doesn't show in Intellisense.)

I have also tried

datePicker1.SelectedDate.Value= new DateTime(inMyString)

and also converting the inMyString to type DateTime but it still does not work.

Any thoughts on how to do this?
Any Suggestions and comments are appreciated.

Thanks!

like image 744
Johnston Avatar asked Jun 18 '11 19:06

Johnston


People also ask

What is the default value of Datepicker?

By default, the DatePicker value is null and the Calendar popup is hidden.

How do I set the value of a kendo Datepicker?

Answers. If you need to update the format of your Kendo UI datepicker, you should be able to use the setOptions function : datepicker. setOptions({ format: "MM/dd/yyyy });

How do I change my Datepicker size?

By default DatePicker has standard height and width (height: '30px' and width: '143px'). You can change this height and width by using height and width property respectively.


2 Answers

Try this:

datePicker1.SelectedDate = new DateTime(2001, 10, 20);

If you need to take datetime from string:

datePicker1.SelectedDate = DateTime.Parse(inMyString);

Side note:

You can replace those 3 lines:

String inMyString;
savedDate = datePicker1.SelectedDate.Value.Date;
inMyString = savedDate.Date.ToShortDateString();

with one:

var inMyString = datePicker1.SelectedDate.Value.ToShortDateString();

Another side note: don't know if there is a reason to it, but you might consider storing datetime as dattime, not as a string.

like image 148
Alex Aza Avatar answered Oct 26 '22 17:10

Alex Aza


If you want to show today's date in WPF C#, use this method:

datePicker1.SelectedDate = DateTime.Today.AddDays(-1);
like image 37
Usman Ali Avatar answered Oct 26 '22 15:10

Usman Ali