【问题标题】:Change date format in Razor在 Razor 中更改日期格式
【发布时间】:2022-01-28 01:49:44
【问题描述】:

我正在编写一个 C# 应用程序,我正在使用实体和 MVC。我的索引视图中有一个表单,用户可以在其中设置要搜索的最小日期和最大日期。但是格式是 MM/dd/YYYY。我想将其设置为 dd/MM/yyyy。

        <div class="col-md-6">
            <form class="form-horizontal" asp-action="SimpleSearch">
                <fieldset>
                    <legend>Simple Search</legend>
                    <div class="form-group">
                        <label for="minDate" class="col-lg-2 control-label">Mini Date</label>
                        <div class="col-lg-10">
                            <input type="date" name="minDate" asp-format="{0:dd/MM/yyyy}"  class="form-control" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="maxDate" class="col-lg-2 control-label">Max Date</label>
                        <div class="col-lg-10">
                            <input type="date" name="maxDate" asp-format="{0:dd/MM/yyyy}"  class="form-control" />
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-lg-10 col-lg-offset-2">
                            <button type="submit" class="btn btn-primary">Submit</button>
                        </div>
                    </div>
                </fieldset>
            </form>
        </div>
    </div>

我试过这个命令,但它不起作用。日期的格式不会改变。

   <input type="date" name="minDate" asp-format="{0:dd/MM/yyyy}"  class="form-control" />

【问题讨论】:

标签: .net asp.net-mvc asp.net-core razor


【解决方案1】:

您是否尝试在 Date 属性上添加以下属性?

[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]

日期类型正确吗?而不是 DateTime?

【讨论】:

    【解决方案2】:

    编辑包含最小和最大日期的模型并设置以下属性:

    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime MinDate {Get; Set;}
    

    别忘了加上:

    using System.ComponentModel.DataAnnotations;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-12
      • 2013-06-03
      • 2015-03-03
      • 2011-11-18
      • 2014-10-23
      • 2016-11-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多