枚举到格式化字符串

发布于 2024-10-24 08:21:43 字数 445 浏览 1 评论 0原文

public enum WebWizDateFormat
{
    DDMMYY,
    MMDDYY,
    YYDDMM,
    YYMMDD
}

 

public class WebWizForumUser
{
    public WebWizDateFormat DateFormat { get; set; }

    public WebWizForumUser()
    {
        this.DateFormat = WebWizDateFormat.DDMMYY;
        HttpContext.Current.Response.Write(this.DateFormat);
    }
}

这是可行的,但是当我response.write时,它需要以“dd/mm/yy”格式出现,我该怎么做?

public enum WebWizDateFormat
{
    DDMMYY,
    MMDDYY,
    YYDDMM,
    YYMMDD
}

 

public class WebWizForumUser
{
    public WebWizDateFormat DateFormat { get; set; }

    public WebWizForumUser()
    {
        this.DateFormat = WebWizDateFormat.DDMMYY;
        HttpContext.Current.Response.Write(this.DateFormat);
    }
}

This works, but when I response.write it needs to come out in the format "dd/mm/yy", how can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一身仙ぐ女味 2024-10-31 08:21:43

简单的答案是不要为此使用枚举。静态类怎么样?

public static class WebWizDateFormat
{
    public const string USFormat = "MM/DD/YY";
    public const string UKFormat = "DD/MM/YY";
}

// . . .
string dateFormat = WebWizDateFormat.USFormat;

(只是一个示例,请将字段重命名为对您有意义的任何名称。)

The simple answer is don't use an enum for this. How about a static class?

public static class WebWizDateFormat
{
    public const string USFormat = "MM/DD/YY";
    public const string UKFormat = "DD/MM/YY";
}

// . . .
string dateFormat = WebWizDateFormat.USFormat;

(Just a sample, rename the fields to whatever makes sense for you.)

生死何惧 2024-10-31 08:21:43

最简单的方法是只使用 Dictionary ,您可以使用枚举的相应字符串表示来填充,即

DateMapping[WebWizDateFormat.DDMMYY] = "dd/mm/yy";

您可以这样做

HttpContext.Current.Response.Write(DateMapping[this.DateFormat]);

Easiest way would be to just use a Dictionary<WebWizDateFormat,string> that you populate with corresponding string represenations for your enum, i.e.

DateMapping[WebWizDateFormat.DDMMYY] = "dd/mm/yy";

then you can just do

HttpContext.Current.Response.Write(DateMapping[this.DateFormat]);
别闹i 2024-10-31 08:21:43

您关于此转换的规则不明确。你可以做类似的事情:

this.DateFormat.ToLower().Insert(4, "\\").Insert(2,"\\");

但我怀疑,这就是你的意思......;-)
这也可能对您有所帮助:带有用户友好字符串的枚举 ToString

Your rules regarding this conversion are not clear. You could do something like that:

this.DateFormat.ToLower().Insert(4, "\\").Insert(2,"\\");

But I doubt, that is what you meant... ;-)
This could also be helpful to you: Enum ToString with user friendly strings

后eg是否自 2024-10-31 08:21:43

序言:我建议不要使用枚举项名称来表示数据(您可以获取给定枚举值和类型的字符串名称)。我还建议使用隐式分配的枚举值,因为添加或删除枚举项之类的细微更改可能会产生细微的不兼容更改/错误。

在这种情况下,我可能只是创建一个从枚举值到字符串格式的映射,例如:

public enum WebWizDateFormat
{
    DDMMYY = 1,
    MMDDYY = 2,
    YYDDMM = 3,
    YYMMDD = 4,
    // but better, maybe, as this abstracts out the "localization"
    // it is not mutually exclusive with the above
    // however, .NET *already* supports various localized date formats
    // which the mapping below could be altered to take advantage
    ShortUS = 10, // means "mm/dd/yy",
    LongUK = ...,
}

public IDictionary<string,string> WebWizDateFormatMap = new Dictionary<string,string> {
    { WebWizDateFormat.DDMMYY, "dd/mm/yy" },
    // "localized" version, same as MMDDYY
    { WebWizDateFormat.ShortUS, "mm/dd/yy" },
    ... // define for -all-
};

// to use later
string format = WebWizDateFormatMap[WebWizDateFormat.ShortUS];

快乐编码

Preamble: I would advise against using an enum item name to represent data (you can get the string name of a given enum value and type). I would also advise using implicitly assigned enum values as subtle changes such as adding or removing an enum item may create subtle incompatible changes/bugs.

In this case I may just create a map from an enum-value to a string format, such as:

public enum WebWizDateFormat
{
    DDMMYY = 1,
    MMDDYY = 2,
    YYDDMM = 3,
    YYMMDD = 4,
    // but better, maybe, as this abstracts out the "localization"
    // it is not mutually exclusive with the above
    // however, .NET *already* supports various localized date formats
    // which the mapping below could be altered to take advantage
    ShortUS = 10, // means "mm/dd/yy",
    LongUK = ...,
}

public IDictionary<string,string> WebWizDateFormatMap = new Dictionary<string,string> {
    { WebWizDateFormat.DDMMYY, "dd/mm/yy" },
    // "localized" version, same as MMDDYY
    { WebWizDateFormat.ShortUS, "mm/dd/yy" },
    ... // define for -all-
};

// to use later
string format = WebWizDateFormatMap[WebWizDateFormat.ShortUS];

Happy coding

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文