asp.net-mvc 在 Asp.NET MVC 中以 dd/mm/yyyy 格式显示日期时间值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18288675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:06:26  来源:igfitidea点击:

Display DateTime value in dd/mm/yyyy format in Asp.NET MVC

asp.net-mvcasp.net-mvc-4datetimedatetime-format

提问by Murat Y?ld?z

Is it possible to display a DateTimevalue in dd/mm/yyyyformat with the help of HTML Helpermethods in Asp.NET MVC? I tried to do this by using some formats in @Html.LabelForand adding some annotations to the related property like below but it does not make any sense. Any help to solve this problem would be appreciated.

是否可以借助 中的方法以dd/mm/yyyy格式显示DateTime值?我试图通过使用一些格式并向相关属性添加一些注释来做到这一点,如下所示,但它没有任何意义。任何解决此问题的帮助将不胜感激。HTML HelperAsp.NET MVC@Html.LabelFor

Model:

模型:

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public Nullable<System.DateTime> RegistrationDate { get; set; }

采纳答案by iYogi

After few hours of searching, I just solved this issue with a few lines of code

经过几个小时的搜索,我只是用几行代码解决了这个问题

Your model

你的模特

      [Required(ErrorMessage = "Enter the issued date.")]
      [DataType(DataType.Date)]
      public DateTime IssueDate { get; set; }

Razor Page

剃刀页

     @Html.TextBoxFor(model => model.IssueDate)
     @Html.ValidationMessageFor(model => model.IssueDate)

Jquery DatePicker

Jquery 日期选择器

<script type="text/javascript">
    $(document).ready(function () {
        $('#IssueDate').datepicker({
            dateFormat: "dd/mm/yy",
            showStatus: true,
            showWeeks: true,
            currentText: 'Now',
            autoSize: true,
            gotoCurrent: true,
            showAnim: 'blind',
            highlightWeek: true
        });
    });
</script>

Webconfig File

网络配置文件

    <system.web>
        <globalization uiCulture="en" culture="en-GB"/>
    </system.web>

Now your text-box will accept "dd/MM/yyyy"format.

现在您的文本框将接受“dd/MM/yyyy”格式。

回答by Mahajan344

All you have to do is apply the format you want in the html helper call, ie.

您所要做的就是在 html helper 调用中应用您想要的格式,即。

@Html.TextBoxFor(m => m.RegistrationDate, "{0:dd/MM/yyyy}")

You don't need to provide the date format in the model class.

您不需要在模型类中提供日期格式。

回答by Dav.id

I know this is an older question, but for reference, a really simple way for formatting dates without any data annotations or any other settings is as follows:

我知道这是一个较旧的问题,但作为参考,在没有任何数据注释或任何其他设置的情况下格式化日期的一种非常简单的方法如下:

@Html.TextBoxFor(m => m.StartDate, new { @Value = Model.StartDate.ToString("dd-MMM-yyyy") })

The above format can of course be changed to whatever.

上述格式当然可以更改为任何格式。

回答by Ayman

Or just use this in your View(Razor page)

或者只是在您的视图中使用它(Razor 页面)

@item.ResgistrationhaseDate.ToString(string.Format("dd/MM/yyyy"))

I recommend that don't add date format in your model class

我建议不要在模型类中添加日期格式

回答by Steven

Since the question was "display" :

由于问题是“显示”:

@Html.ValueFor(model => model.RegistrationDate, "{0:dd/MM/yyyy}")

回答by Jehad87

You need to use html helper, and you don't need to provide date format in model class. e.x :

您需要使用 html helper,并且不需要在模型类中提供日期格式。前任 :

@Html.TextBoxFor(m => m.ResgistrationhaseDate, "{0:dd/MM/yyyy}")

回答by Ramy M. Mousa

It might be too late to answer this in 2019. but I tried all the answers and none worked for me. So I solved it simply this way:

2019 年回答这个问题可能为时已晚。但我尝试了所有的答案,但没有一个对我有用。所以我简单地解决了它:

@Html.EditorFor(m => m.SellDateForInstallment, "{0:dd/MM/yyyy}", 
               new {htmlAttributes = new { @class = "form-control", @type = "date" } })

EditorForis what worked for me.

EditorFor是什么对我有用。

Note that SellDateForInstallmentis a Nullabledatetime object.

请注意,这SellDateForInstallment是一个Nullable日期时间对象。

public DateTime? SellDateForInstallment { get; set; } // Model property