In ASP.NET, how can I force the format of dates in a DropDownList to "DD/MM/YYYY"?
Solution 1
I would wrap the DateTime in another object and override ToString() as it is what the dropdownlist displays.
class MyDateTime {
public MyDateTime(DateTime dt) {
_dt = dt;
}
public override String ToString() {
return _dt.ToString("dd/MM/yyyy");
}
private DateTime _dt;
}
The main advantage of doing so is that you can store other information than just a string to reference other objects or data. If only a plain string is sufficient it is overkill.
If having a '/' is important for you in all locales (languages) then you have to upquote it otherwise you might end up with another character in some locates.
See http://www.color-of-code.de/index.php?option=com_content&view=article&id=58:c-format-strings&catid=38:programming&Itemid=66 for some examples (my cheat list with gotchas I ran into).
The code has to be modified a little bit:
DateTime date = DateTime.Now;
List<MyDateTime> dates = new List<MyDateTime>();
for (int i = 0; i < HISTORY_LENGTH; i++)
{
dates.Add(new MyDateTime(date.AddDays(-i)));
}
DropDownList.DataSource = dates;
DropDownList.DataBind();
Solution 2
All you need to do is set DropDownList.DataTextFormatString
- then upon DataBinding your control will apply the correct format:
<asp:DropDownList
id="yourList"
runat="server"
dataTextFormatString="{0:dd/MM/yyyy}"/>
Solution 3
Instead of formatting the datasource you can also set format of the date as :
DropDownList.DataTextFormatString = "{0:dd/MM/yyyy}";
Solution 4
Format the Dates in the list that way before you bind the data to the control.
Solution 5
List<string> dates = new List<string>(HISTORY_LENGTH - 1);
for (int i = 0; i < HISTORY_LENGTH; i++)
{
dates.Add(DateTime.Today.ToString("dd/MM/yyyy"));
}
DropDownList.DataSource = dates;
DropDownList.DataBind();
Related videos on Youtube
Bastien Vandamme
Updated on April 17, 2022Comments
-
Bastien Vandamme about 2 years
I have to build a DropDownList with the dates of the last 7 days. I would like the DropDownList displays the date as "DD/MM/YYYY". So I created a list of dates:
DateTime date = DateTime.Now; List<DateTime> dates = new List<DateTime>(); for (int i = 0; i < HISTORY_LENGTH; i++) { dates.Add(date.AddDays(-i)); } DropDownList.DataSource = dates; DropDownList.DataBind();
I add my dates as DateTime, not as strings. I think it is the method ToString() of my DateTime object that is called to create text that is visible in my DropDownList. By default it is the date and time. The result is:
[0]: {16/07/2008 11:08:27}
[1]: {15/07/2008 11:08:27}
[2]: {14/07/2008 11:08:27}
[3]: {13/07/2008 11:08:27}
[4]: {12/07/2008 11:08:27}
[5]: {11/07/2008 11:08:27}
[6]: {10/07/2008 11:08:27}
How can I force the format to "DD/MM/YYYY"?
-
jdehaan over 14 yearsA bit high maybe but once he wants to associate other things with the dates, might prove to be helpful.
-
o.k.w over 14 yearshigh? haha, I would call it forward thinkng for reusability.
-
Alan Macdonald over 11 yearsThis doesn't work as you get an exception saying the instance is read only. See stackoverflow.com/questions/7413077/… or stackoverflow.com/questions/10332064/… showing you need to clone or construct a new culture object.