MVC3& Razor - 将日期时间字符串从“mm/dd/yyyy 12:00:00 AM”更改为至“mm/dd/yyy”在文本框中

发布于 2024-10-17 02:58:47 字数 837 浏览 5 评论 0原文

我试图阻止时间显示在使用 DateTime 的生日文本字段中

我的代码:(我正在使用 Jquery DatePicker)

<label for="birthday">Birthday:</label>
            @Html.TextBox("birthday", (Model.Birthday.ToString()), new { @class = "datePicker" })

Javascript:

$(document).ready(function () {
    $('.datePicker').datepicker({ showOn: 'both', buttonImage: "/content/images/calendar-red.gif" });
});

我有日期的模型设置:

[DataType(DataType.Date)]
public DateTime? Birthday { get; set; }

仍然是文本中的文本文本框显示:

“8/21/2010 12:00:00 AM”

我希望文本框中的文本显示为:

“8/21/2010”

I已经尝试过以下所有内容:

@inherits System.Web.Mvc.WebViewPage<System.DateTime>

但不会让我这样做,因为我@使用模型

I am trying to stop the Time from showing up in a Birthday Text Field that uses DateTime

My Code: (I'm using the Jquery DatePicker)

<label for="birthday">Birthday:</label>
            @Html.TextBox("birthday", (Model.Birthday.ToString()), new { @class = "datePicker" })

The Javascript:

$(document).ready(function () {
    $('.datePicker').datepicker({ showOn: 'both', buttonImage: "/content/images/calendar-red.gif" });
});

I have the Model setup for the date:

[DataType(DataType.Date)]
public DateTime? Birthday { get; set; }

Still the Text in the Text box displays:

"8/21/2010 12:00:00 AM"

I want Text in the Textbox to diplay as just:

"8/21/2010"

I have tried everything from:

@inherits System.Web.Mvc.WebViewPage<System.DateTime>

but wont let me do that since I am @using a model

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岁吢 2024-10-24 02:58:47

我将在视图模型上使用编辑器模板和数据注释来指定格式,使视图更加清晰:

[DataType(DataType.Date)]
[DisplayName("Birthday:")]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime? Birthday { get; set; }

然后在视图中:

<span class="datePicker">
    @Html.LabelFor(x => x.Birthday)
    @Html.EditorFor(x => x.Birthday)
</span>

然后调整选择器,因为文本框将不再具有 datePicker班级:

$('.datePicker :text').datepicker({ 
    showOn: 'both', 
    buttonImage: "/content/images/calendar-red.gif" 
});

I would use an editor template and data annotations on the view model to specify formatting which makes the views much cleaner:

[DataType(DataType.Date)]
[DisplayName("Birthday:")]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime? Birthday { get; set; }

and then inside your view:

<span class="datePicker">
    @Html.LabelFor(x => x.Birthday)
    @Html.EditorFor(x => x.Birthday)
</span>

and then adapt the selector because the textbox will no longer have the datePicker class:

$('.datePicker :text').datepicker({ 
    showOn: 'both', 
    buttonImage: "/content/images/calendar-red.gif" 
});
ぃ双果 2024-10-24 02:58:47

尝试更改为:

@Html.TextBox("birthday", (Model.Birthday.Value.ToShortDateString()), new { @class = "datePicker" })

因为您的 DateTime 是 Nullable 您需要执行 .Value 才能访问 DateTime 方法。

当在 DateTime 上调用 ToString 时,它会给你日期 + 时间。因此,您不想调用 ToString,而是使用 ToString 带格式ToShortDateString

MSDN 对 ToShortDateString 有以下说法

返回的字符串
ToShortDateString 方法是
文化敏感。它反映了
由当前定义的模式
文化的 DateTimeFormatInfo 对象。
例如,对于 en-US 文化,
标准的短日期模式是
“月/日/年”;对于去DE文化来说,
是“dd.MM.yyyy”;对于 ja-JP
文化,它是“yyyy/M/d”。这
特定的特定格式字符串
电脑也可以定制,所以
它与标准不同
短日期格式字符串。

Try changing to this:

@Html.TextBox("birthday", (Model.Birthday.Value.ToShortDateString()), new { @class = "datePicker" })

Since your DateTime is Nullable you need to do .Value before you can access the DateTime methods.

When calling ToString on a DateTime it will give you Date + Time. So Instead of calling ToString you want to use either ToString with formatting or ToShortDateString.

MSDN has the following to say about ToShortDateString

The string returned by the
ToShortDateString method is
culture-sensitive. It reflects the
pattern defined by the current
culture's DateTimeFormatInfo object.
For example, for the en-US culture,
the standard short date pattern is
"M/d/yyyy"; for the de-DE culture, it
is "dd.MM.yyyy"; for the ja-JP
culture, it is "yyyy/M/d". The
specific format string on a particular
computer can also be customized so
that it differs from the standard
short date format string.

嘴硬脾气大 2024-10-24 02:58:47

我实际上在使用 MVC 3 的 Razor 中遇到了这个问题。我最终不得不在 Shared/EditorTemplates 中创建一个 DateTime 部分类来获取我需要的属性。

以前我的观点是: @Html.TextBoxWithAttributesFor(m => m.To, new { @class = "text-box", @readonly = "readonly" }) 但这保留了给了我时间戳。即使我尝试了 .Value.DateTime 或任何其他解决方法。但我当前的工作解决方案是:

view:

@Html.EditorFor(m => m.To)

partial class:

@model System.DateTime
@Html.TextBox("", (Model.ToShortDateString()), new { @class = "text-box", @readonly = "readonly" })

我使用此页面作为第二个来源:
http:// buildstarted.com/2010/09/10/overriding-displayfor-and-editorfor-to-create-custom-outputs-for-mvc/

I was actually running into this problem with Razor using MVC 3. I ended up having to create a DateTime Partial Class in Shared/EditorTemplates to get the attributes I needed.

Previously my view was: @Html.TextBoxWithAttributesFor(m => m.To, new { @class = "text-box", @readonly = "readonly" }) but this kept given me the TimeStamp. Even when I tried .Value.DateTime or any other work around. But my current, working solution is:

view:

@Html.EditorFor(m => m.To)

partial class:

@model System.DateTime
@Html.TextBox("", (Model.ToShortDateString()), new { @class = "text-box", @readonly = "readonly" })

I used this page as a second source:
http://buildstarted.com/2010/09/10/overriding-displayfor-and-editorfor-to-create-custom-outputs-for-mvc/

み零 2024-10-24 02:58:47

如果您有“Model.Birthday.ToString()”,则需要将其替换为“Model.Birthday.ToShortDateString()”

Where you have "Model.Birthday.ToString()" you need to replace it with "Model.Birthday.ToShortDateString()"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文