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"?
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();
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}"/>
Instead of formatting the datasource you can also set format of the date as :
DropDownList.DataTextFormatString = "{0:dd/MM/yyyy}";
Format the Dates in the list that way before you bind the data to the control.
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();
Just manually add the items to the DropDownList.Items collection instead of relying on DataBind():
DateTime date = DateTime.Now;
for (int i = 0; i < HISTORY_LENGTH; i++)
{
DropDownList.Items.Add(new ListItem(date.AddDays(-i).ToString("dd/MM/yyyy"), date.AddDays(-i)))
}
If this is something you have to do across your entire app you need to be looking at the CultureInfo object.
You get the current CultureInfo object by calling
var culture = CultureInfo.CurrentCulture;
The CultureInfo
object has a property called DateTimeFormat
which in turn has a property called ShortDatePattern
which you should set like so...
culture.DateTimeFormatInfo.ShortDatePattern = "dd/MM/yyyy";
Now you can use that anywhere by formatting the string like so...
String.Format("{d}", someDateTime);