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

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

问题描述

我必须建立一个DropDownList与过去7天的日期。我希望DropDownList将日期显示为DD / MM / YYYY。所以我创建了一个日期列表:

  DateTime date = DateTime.Now; 
列表< DateTime> dates = new List< DateTime>(); (int i = 0; i< HISTORY_LENGTH; i ++)
{
dates.Add(date.AddDays(-i));


}

DropDownList.DataSource = dates;
DropDownList.DataBind();

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


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


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

解决方案

我将DateTime放在另一个对象中覆盖ToString(),就像下拉列表显示的那样。

  class MyDateTime {
public MyDateTime(DateTime dt){
_dt = dt;
}
public override String ToString(){
return _dt.ToString(dd / MM / yyyy);
}
private DateTime _dt;
}

这样做的主要优点是可以存储其他信息,而不仅仅是字符串引用其他对象或数据。如果只有一个简单的字符串就足够了,那就是过度的。



如果在所有语言环境(如语言)中有一个/对你很重要,那么你必须加注它,否则你可能会在某些地方找到另一个角色。



请参阅 http://www.color-of-code.de/index。一些例子(我的作弊列表,我遇到的问题),php?option = com_content& view = article& id = 58:c-format-strings& catid = 38:programming& Itemid = 66 。 >

代码必须修改一下:

  DateTime date = DateTime 。现在; 
列表< MyDateTime> dates = new List< MyDateTime>(); (int i = 0; i< HISTORY_LENGTH; i ++)
{
dates.Add(new MyDateTime(date.AddDays(-i)));


}

DropDownList.DataSource = dates;
DropDownList.DataBind();


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

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

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