只显示日期和时间不 [英] Display only date and no time

查看:198
本文介绍了只显示日期和时间不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MVC剃刀,我把当前的日期在数据库这样的..

In MVC razor, I am putting current date in the database like this..

model.Returndate = DateTime.Now.Date.ToShortDateString();

由于数据库字段是datetime数据类型,我当前日期转换为字符串格式,这是行不通的。我该怎么办呢?我做的字符串格式,因为我想为mm / dd / yyyy格式的日期,而不是​​在MM / DD / YYYY HH:MM:SS时间格式。

Since the database field is a datetime datatype and I am converting the current date to string format, this is not working.. how can I do this? I am doing the string format because I want the date in mm/dd/yyyy format and not in mm/dd/yyyy hh:mm:ss time format..

编辑:

在控制器我有

var model = new ViewModel();
model.ReturnDate = DateTime.Now;            
return PartialView("PartialView", model);  

在partialview,我有

In the partialview, I have

@Html.EditorFor(model => model.Returndate)

这就是它的显示日期,日期和时间在一起......我希望显示只是日期。还不是时候。我希望这个编辑解释更好。

This is where its displaying the date as Date and Time together... I want just the date to be displayed. Not the time. I hope this edit explains better.

推荐答案

如果列类型是日期时间在SQL那么它将存储在您传递一个或没有时间。

If the column type is DateTime in SQL then it will store a time where you pass one or not.

这将会是更好的妥善保存日期:

It'd be better to save the date properly:

model.ReturnDate = DateTime.Now;

当你需要显示它,然后格式化:

and then format it when you need to display it:

@Html.Label(Model.ReturnDate.ToShortDateString())

或者,如果你使用EditorFor:

Or if you're using EditorFor:

@Html.EditorFor(model => model.ReturnDate.ToShortDateString())

@Html.EditorFor(model => model.ReturnDate.ToString("MM/dd/yyyy"))

要一个属性添加到您的模型中添加此code:

To add a property to your model add this code:

public string ReturnDateForDisplay
{
    get
    {
       return this.ReturnDate.ToString("d");
    }
}

然后在您的PartialView:

Then in your PartialView:

@Html.EditorFor(model => model.ReturnDateForDisplay)

编辑:

大家好,只是想澄清这个答案由我说'如果你使用EditorFor',这意味着你需要为价值你想重新present类型的EditorFor模板

Hi guys, just want to clarify for this answer that by my saying 'If you're using EditorFor', that means you need to have an EditorFor template for the type of value you're trying to represent.

编辑模板是MVC管理重复控制一个很酷的方式:

Editor templates are a cool way of managing repetitive controls in MVC:

<一个href=\"http://coding-in.net/asp-net-mvc-3-how-to-use-editortemplates/\">http://coding-in.net/asp-net-mvc-3-how-to-use-editortemplates/

您可以使用他们的天真的类型,如String作为我之前所做的那样;但他们对让你模板一组输入字段用于更复杂的数据类型,尤其是巨大的。

You can use them for naive types like String as I've done above; but they're especially great for letting you template a set of input fields for a more complicated data type.

这篇关于只显示日期和时间不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆