通过SQL Server从返回的日期和时间中仅提取日期以显示在网页中 [英] Extracting just date from returned date and time by SQL server to show in web pages

查看:95
本文介绍了通过SQL Server从返回的日期和时间中仅提取日期以显示在网页中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从sql server中检索日期,并希望在我的视图/网页中将日期显示为"2014年11月20日".问题是SQL Server返回了日期和时间(2014年12月3日12:00:00 AM).以下是我的控制器操作和视图

I retrieve the date from sql server and want to display the date as "November 20, 2014" in my view / web page. The problem is SQL server returned the date and time (12/3/2014 12:00:00 AM). Below is my controller action and view

控制器

public ActionResult Index()
        {
            NewsViewModel NewsVM = new NewsViewModel(); //Initializing the view model
            var news = db.WASA_News.ToList();// getting all news from database
            NewsVM.WasaNews = news; //Adding the news into the viewmodel

            return View(NewsVM);// Returning ViewNodel
        }

局部视图

<div class="col-md-9 animated fadeInLeft">
                      <span>@Html.DisplayFor(modelItem => item.News_Date)</span>
                      <span>@Html.DisplayFor(modelItem => item.News_Title)</span>
                      <p>@Html.DisplayFor(modelItem => item.News_Description)</p>
 </div>

有关以所需格式提取日期的任何帮助.

any help regarding the extracting the date on desired format.

推荐答案

您只需要格式化日期. <span>@Model.item.News_Date.ToString("d")</span>应该仅格式化日期.

You just need to format the date. <span>@Model.item.News_Date.ToString("d")</span> should format the date only.

如果您可以编辑模型,则可以选择添加DisplayFormat属性:

If you can edit your model, you can alternatively add the DisplayFormat attribute:

[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
public DateTime News_Date { get; set; }

如果您要坚持使用HTML帮助程序方法,则可以使用Value帮助程序.

And if you want to stick with the Html helper method, you can use the Value helper.

@Html.ValueFor(modelItem => modelItem.News_Date, "{0:MM/dd/yyyy}")

这篇关于通过SQL Server从返回的日期和时间中仅提取日期以显示在网页中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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