In ASP.NET, how can I force the format of dates in a DropDownList to “DD/MM/YYYY”?

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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}"/>

Method 2

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

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

Method 3

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

Method 4

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();

Method 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();

Method 6

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)))
}

Method 7

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x