在ASP.NET中,我怎么能强迫日期格式在DropDownList中到" DD / MM / YYYY"? [英] In ASP.NET, how can I force the format of dates in a DropDownList to "DD/MM/YYYY"?

查看:148
本文介绍了在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对象的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(),因为它是DropDownList的显示。

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.

如果有一个'/'是在所有的语言环境(语言),那么你必须,否则upquote这对你很重要,你可能最终在某些所处另一个角色。

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-$c$c.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).

在code,必须修改一点点:

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中到&QUOT; DD / MM / YYYY&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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