I am using asp.net webforms.There is no date time picker control provided. Presently I’m using a textbox to display datetime. I want a date time picker control. I tried to google, couldn’t find a solution. Everything google gives is paid control made by different companies.
I tried searching for a solution to find free date time picker control. I’m not successful. I want a solution to get a free date time picker control.I’m able to get a date picker control by adding type=date on a regular textbox. But date and time picker control is what I want.
Edit:
I added datetime-local. It gives me a date time picker control. Awesome!
<asp:TextBox ID="TextBoxDateTime" runat="server" type="datetime-local"></asp:TextBox>
I’m almost close to a solution. I still have one small issue which I need help though. The date time format it outputs is little weird. The output for the selected date for nov 2, 2023, 5 pm is 2023-11-02T17:00. The output is totally weird. The output is in yyyy-mm-dd format. There is an additional T added just before the time. I need a help from the community now. I want to insert this value in a sql server database. In this present format, that is 2023-11-02T17:00, I can’t insert this value into database. Can someone help me convert this weird format into database friendly date time?
2
Answers
This line does the trick. type=datetime-local added the date time picker. Just an easy solution. But the TextBoxDateTime.Text gives out the value in ISO string format. To convert it into .NET datetime, use the following code.
First let’s correct some false statements here.
quote:
Wrong!!!! There is most certainly here is a built in calendar control you can use, and there is ALSO options for the standard text box control.
Let’s first use the standard text box control.
When you drag + drop in a text box to the webform, then note the property sheet settings for that text box. These ones:
So, there are all kinds of choices (time picker, password, number and more), and there are several date choices. So, in above, we choose date from the drop down.
So, say with this markup:
And code behind so far is this simple stub:
So, running the above, we get/see this:
And for output window, we see this:
(I hit the button 2 times).
I should also point out that if you typing in markup (as opposed to using the property sheet as shown above), then you can type in textmode, hit = and then hit ctrl-space (as always for inteli-sense), and again you get the list of options for the text box.
This:
So, CLEARLY without any add-ins, without any JavaScript code, asp.net supports and has a date picker ability out of box. Note how the date displays in the browsers local time, but to get (or set) the text box, you need to use ISO date format.
So, say on page load, I could have the date "default" for today like this:
(note the all important test for isPostBack, since we ONLY want such defaults and setup code to run on first page load – not for additional post-backs.
Next up, if you look at the tool box, you should see a calendar control
This one:
So, we could drag that onto the web page, but that calendar control is not a auto-popup control, but remains in full view, so you might say drag 2 copies onto the web page. Not that the calendar control has EXTENSIVE formatting options like for weekdays, weekend, today, and more.
So, we drag + drop 2 of the controls onto the form, and we now have this markup:
And when we run the above, we have this:
And the code behind can be this:
And output:
(note how the calendar control DOES return a datetime data type).
So, you can use a text box, just set the TextMode="date".
And as noted, you can also use a calendar control.
So yes, asp.net textbox has a date mode. (it also has a time mode too!!!).
And as above shows, from the toolbox, there is also a calendar control you can use.