문제

So basically, what I want is to format DateTime in whole application and show it in a specific format in the whole Web App. Is there any better solution to just write ToString("{0:stringformathere}", obj)?

도움이 되었습니까?

해결책

Try to override InitializeCulture method for Web page.

Thread.CurrentThread.CurrentCulture = <some custom culture>

http://msdn.microsoft.com/en-us/library/bz9tc508.aspx

다른 팁

You can create your own Editor/Display Template and reuse across the application.

Views/Shared/EditorTemplates/CustomDateTime.cshtml

@model DateTime?
@{
    String modelValue = "";
    var dateFormatToDisplay =
       System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern;

    if (Model.HasValue)
    {
        if (Model.Value != DateTime.MinValue)
        {
            modelValue = Model.Value.ToString(dateFormatToDisplay);
        }
    }
}

@Html.TextBox("", modelValue)

In Views

@Html.EditorFor(m => m.DateOfBirth, "CustomDateTime")
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top