【问题标题】:How to display date from datetime while retrieving the datetime from database in mvc5?如何在 mvc5 中从数据库中检索日期时间时显示日期时间中的日期?
【发布时间】:2016-08-09 23:20:07
【问题描述】:

我想在从数据库中检索数据时将日期时间转换为日期格式。

我的控制器代码

public ActionResult GetDates(VisitorsViewModel VisitorsVM)
{
    try
    {
        string sqlcmd = "Select * from View_VisitorsForm where CONVERT(VisitingDate As Date)  >='" + fromdt + "'and CONVERT( VisitingDate As Date) <= '" + todt + "'";

        con.Open();
        SqlCommand cmd = new SqlCommand(sqlcmd, con);
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        adp.Fill(dt);
    }
    catch (Exception ex)
    {
        throw;
    }

    ReportDocument rpt = new ReportDocument();
    rpt.Load(Server.MapPath("~/Areas/Sales/CrystalReports/rpt_VisitSummaryCrystalReport.rpt"));
    rpt.SetDataSource(dt);
    Stream stream = rpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);

    return File(stream, "application/pdf");
}

我想在从数据库检索时转换日期格式(只需要日期而不是时间)

如何将 VisitingDate 转换为仅在下面的查询中显示日期(而不是时间)

string sqlcmd = "Select * from View_VisitorsForm where CAST( VisitingDate As Date)  >='" + fromdt + "'and CAST( VisitingDate As Date) <= '" + todt + "'";

上面的sql查询不能转换日期格式,它是随时间而来的。我只想要 Date 。例如:12-Mar-16 像这样。

【问题讨论】:

  • CONVERT(VARCHAR(8),VisitingDate , 1) .. 在您的选择状态中
  • @ShekharPankaj 时间到了

标签: c# asp.net-mvc-5


【解决方案1】:

使用 ToString 仅获取日期,假设您在 date 变量中有 DateTime 类型,那么您可以通过此获取

string s = date.ToString("dd/MM/yyyy");

如果您使用的是 sql server 2008 或更高版本,那么您只能通过 CONVERT(VisitingDate, getdate()) 获取日期

string sqlcmd = "Select * from View_VisitorsForm where CONVERT(VisitingDate, getdate(),110)  >='" + fromdt + "'and CONVERT(VisitingDate, getdate(),110) <= '" + todt + "'";

【讨论】:

  • 我想在下面的查询字符串中转换这个 sqlcmd = "Select * from View_VisitorForm where CAST(VisitingDate As Date) >='" + fromdt + "'and CAST(VisitingDate As Date)
  • CONVERT(VisitingDate, getdate()) 也会显示时间。
  • @Mostadiz 好的,我试试你更新评论并告诉你
  • @RahulMakwana 是的,它会显示时间部分,但对于每个人来说都是静态的 12:00:00 AM
  • @Mostadiz 它不工作你更新的查询显示空错误(类型 VisitingDate 不是定义的系统类型。)这个错误来了
【解决方案2】:

在 sql 中,您可以在 SQL 查询中使用CONVERT() 函数来完成这项工作。

CONVERT(VARCHAR(19),GETDATE())    
CONVERT(VARCHAR(10),GETDATE(),10)
CONVERT(VARCHAR(10),GETDATE(),110)
CONVERT(VARCHAR(11),GETDATE(),6)
CONVERT(VARCHAR(11),GETDATE(),106)
CONVERT(VARCHAR(24),GETDATE(),113)

以上将导致:

2014 年 11 月 4 日晚上 11:45

11-04-14 11-04-2014

14 年 11 月 4 日

2014 年 11 月 4 日

2014 年 11 月 4 日 11:45:34:243

所以您的查询将是:

string sqlcmd = "Select Column1,Column2,Column3,CONVERT(VARCHAR(10),VisitingDate,110) as 'VisitingDate' from View_VisitorsForm where CONVERT(VARCHAR(10),VisitingDate,110)  >='" + fromdt + "'and CONVERT(VARCHAR(10),VisitingDate,110) <= '" + todt + "'";

这是您的问题,您需要选择所有列并为日期列执行此操作。所以在查询中用你的列名替换 Column1,column2。

查看 w3Schools 教程here

【讨论】:

  • 我想改变前端的sql查询
  • 是的,我有一个检查并尝试
  • 只有 Mostafiz 给出了相同的答案,但它显示为 null(类型 VisitingDate 不是已定义的系统类型。)错误
  • 时间到了@rahul Makwana,它和图片中提到的一样
  • 抱歉犯了愚蠢的错误,我意识到您在选择日期时没有转换日期,更新后的查询会在我们对视图进行选择时转换它。
猜你喜欢
  • 2020-06-02
  • 2019-04-24
  • 1970-01-01
  • 2021-07-12
  • 2011-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多