日期格式与Html助手

日期格式与Html助手

本文介绍了日期格式与Html助手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的mvc应用程序中,我使用 DisplayFor Htmlhelper将 Date 赋值给我的视图,如下所示。我试图像这样格式化。但是,我没有成功。

  @ Html.DisplayFor(d => d.TravelAllowances.DepatureDate,{0: dd-MM-yy})

请给我一些方法来完成这个。


$ b

注意: DepartureDate 位于字符串格式中。



感谢
Manikandan

解决方案

我知道这个问题已经三年了, OP可能找到了一个方法,但万一别人偶然发现这个问题...



问题是,你只有一个字符串的日期。

如果您将它作为日期存储,并在从控制器填充传递给视图的模型时将其转换为字符串,则可以根据需要将其格式化为控制器。

如果你将日期存储为一个字符串(坏主意恕我直言),只是在控制器中填充模型属性,那么我会解析字符串到一个DateTime对象,然后将该DateTime格式化为模型属性的字符串。



仅仅是我的两分钱,晚了三年:)

In my mvc application, i assign the Date to my view using DisplayFor Htmlhelper, like below. I tried to format like this. But, i am not successful.

@Html.DisplayFor(d => d.TravelAllowances.DepatureDate, "{0:dd-MM-yy}")

Please suggest me some way to accomplish this.

Note: DepartureDate is in String format.

ThanksManikandan

解决方案

I know this question is three years old and the OP probably found a way, but in case others stumble upon this Q/A...

The problem is that you have the date as just a string.

If you are storing it as a date and converting it to a string when populating the model being passed to the view from the controller, then format it as you want in the controller.

If you are storing date as a string (bad idea IMHO), and just populating the model property in the controller, then I would parse the string into a DateTime object and then format that DateTime into a string for the Model's property.

Just my two cents, three years late :)

这篇关于日期格式与Html助手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 00:20