在 ASP.NET 中,如何将 DropDownList 中的日期格式强制为“DD/MM/YYYY"? [英] In ASP.NET, how can I force the format of dates in a DropDownList to "DD/MM/YYYY"?

查看:15
本文介绍了在 ASP.NET 中,如何将 DropDownList 中的日期格式强制为“DD/MM/YYYY"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须用过去 7 天的日期构建一个 DropDownList.我希望 DropDownList 将日期显示为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();

我将日期添加为 DateTime,而不是字符串.我认为是我的 DateTime 对象的 ToString() 方法被调用以创建在我的 DropDownList 中可见的文本.默认情况下,它是日期和时间.结果是:

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}

[0]: {16/07/2008 11:08:27}

[1]:{15/07/2008 11:08:27}

[1]: {15/07/2008 11:08:27}

[2]:{14/07/2008 11:08:27}

[2]: {14/07/2008 11:08:27}

[3]:{13/07/2008 11:08:27}

[3]: {13/07/2008 11:08:27}

[4]:{12/07/2008 11:08:27}

[4]: {12/07/2008 11:08:27}

[5]:{11/07/2008 11:08:27}

[5]: {11/07/2008 11:08:27}

[6]:{10/07/2008 11:08:27}

[6]: {10/07/2008 11:08:27}

如何强制格式为DD/MM/YYYY"?

How can I force the format to "DD/MM/YYYY"?

推荐答案

我会将 DateTime 包装在另一个对象中并覆盖 ToString(),因为它是下拉列表显示的内容.

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.

http://www.color-of-code.de/index.php?option=com_content&view=article&id=58:c-format-strings&catid=38:programming&Itemid=66 一些例子(我的作弊清单和我遇到的问题).

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

这篇关于在 ASP.NET 中,如何将 DropDownList 中的日期格式强制为“DD/MM/YYYY"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆