在ASP.NET Core MVC中的视图中格式化日期

时间:2018-11-23 10:30:35

标签: asp.net-core-mvc

如何将日期格式设置为"dd.MM.yyyy",因为我所做的总是"MM/dd/yyyy"

查看部分:

<div class="col-md-6 col-xs-6 pull-left">
    <label asp-for="@Model.RequestRegistrationDateFrom"></label>
    <div class="input-group date">
        <input type="text" asp-for="@Model.RequestRegistrationDateFrom" class="form-control js-datepicker pull-left" />
        <div class="input-group-append">
            <span class="input-group-text calendar-fa"></span>
        </div>
    </div>
</div>

模型属性:

[BindProperty(SupportsGet = true)]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "dd.MM.yyyy")]
[Display(Name = "Date from", ResourceType = typeof(string))]
public DateTime? RequestRegistrationDateFrom { get; set; }

3 个答案:

答案 0 :(得分:1)

这应该有效:

[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]

答案 1 :(得分:1)

我听说过您遇到的情况。不幸的是,我自己没有得到它,也没有机会深入研究DisplayFormat属性背后的实际问题。

在这种情况下,将格式化的字符串传递给value属性是可行的。

class scrollView extends Component {
    constructor(props) {
        super(props);
        this.setScrollSectionRef = this.setScrollSectionRef.bind(this);
        this.state = {
            scrollSection: null,
        };
    }
    setScrollSectionRef(node) {
        const { scrollSection } = this.state;
        this.scrollSection = node;
        // this is to force a re-render of messages
        // for them to recive the scrollSectionRef
        if (!scrollSection) {
            this.setState({ scrollSection: this.scrollSection });
        }
    }
    render() {
        return (
            <section ref={this.setScrollSectionRef} className="threadMessageListContainer">
                <ul className="threadMessageList">
                    {
                        this.props.data.map((data) =>
                            <MessageCell
                                key={data.id}
                                scrollSection={scrollSection}
                            />
                        )
                    }
                </ul>
            </section>
        )
    }
}

class MessageCell extends Component {
    constructor(props) {
        super(props);
        this.setLIref = this.setLIref.bind(this);
    }
    componentDidMount() {
        // Auto scroll window to bottom of scrollView feed
        if (this.liRef) {
            this.liRef.scrollIntoView();
        }
    }
    setLIref(node) {
        this.liRef = node;
    }
    render() {
        return (
            <li
                ref={this.setLIref}
                className={cellClasses}
            >
                some displayed data
            </li>
        )
    }
}

暂时尝试。希望它也适用于您的情况。

如果您找出问题所在时可以在此处发布解决方案,那就太好了。我对此感兴趣。

答案 2 :(得分:0)

在您的模型中:

[Column(TypeName = "Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
public DateTime start_date { get; set; }

您认为:

<td>@item.start_date.ToString("dd/MM/yyyy")</td>