views:

594

answers:

7

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"?

A: 

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&amp;view=article&amp;id=58:c-format-strings&amp;catid=38:programming&amp;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();
jdehaan
Sry that's very high to do for such an simple thing...
Kovu
A bit high maybe but once he wants to associate other things with the dates, might prove to be helpful.
jdehaan
high? haha, I would call it forward thinkng for reusability.
o.k.w
+2  A: 

Format the Dates in the list that way before you bind the data to the control.

duffymo
+1  A: 
   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();
Jamie Ide
A: 

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)))
}
iAn
+9  A: 

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}"/>
Andrew Hare
+2  A: 

Instead of formatting the datasource you can also set format of the date as :

DropDownList.DataTextFormatString = "{0:dd/MM/yyyy}";
Canavar
A: 

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);
Jason Punyon