仅显示日期,没有时间 [英] Display only date and no time

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

问题描述

  model.Returndate = DateTime.Now在MVC剃须刀中,我将当前日期放在数据库中。 .Date.ToShortDateString(); 

由于数据库字段是datetime数据类型,我将当前日期转换为字符串格式,不工作..我该怎么办?我正在做字符串格式,因为我想要的日期是mm / dd / yyyy格式,而不是mm / dd / yyyy hh:mm:ss时间格式。



编辑:



在控制器中我有

  var model = new ViewModel(); 
model.ReturnDate = DateTime.Now;
返回PartialView(PartialView,model);

在部分视图中,我有

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

这是日期和时间一起显示的地方...我只想显示日期。不是时候我希望这个编辑更好地解释。

解决方案

如果列类型是SQL中的DateTime,那么它将存储一个你通过一个或者不是。



最好保存日期正确:

  model.ReturnDate = DateTime.Now; 

,然后在需要显示时格式化:

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

或者如果您使用EditorFor:

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

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

要向模型添加属性,请添加以下代码:

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

然后在您的PartialView中:

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

$只是想澄清一下这个答案,我说的如果你使用EditorFor',这意味着您需要为您想要表示的值类型提供一个EditorFor模板。



编辑器模板是管理MVC中重复性控件的一种很酷的方法: / p>

http://coding-in.net/asp-net-mvc-3-how-to-use-editortemplates/



您可以使用他们对于像我上面所做的字符串的天真类型;但是它们特别适合让您为更复杂的数据类型模板一组输入字段。


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

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

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..

EDIT:

In the controller I have

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

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.

解决方案

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())

Or if you're using EditorFor:

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

or

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

To add a property to your model add this code:

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

Then in your PartialView:

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

EDIT:

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.

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

http://coding-in.net/asp-net-mvc-3-how-to-use-editortemplates/

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天全站免登陆