【问题标题】:I need to change the format of date while retrieving from database我需要在从数据库中检索时更改日期格式
【发布时间】:2015-10-15 20:54:18
【问题描述】:

我有这个代码来填充我的网格:

protected void fillGrid()
    {
        GridView1.DataSource = (DataTable)db.ExecuteSql("select b.*,bl.StartDate,bl.EndDate from " + schemaname + ".ClaimMaster b," + schemaname + ".ClaimMaster_Link bl where b.IsDeleted=0 and b.ClaimCode=bl.ClaimCode order by ClaimCode desc ", "");
        GridView1.DataBind();
    }

正在检索的日期格式为dd/mm/yyyy。 数据库中存储的数据是2015-07-02 17:15:47.000

但我需要使用dd/mm/yyyy 格式的日期填充我的gridview

【问题讨论】:

  • 你需要它是字符串还是 DateTime 对象?
  • 将日期加载到日期时间变量,然后使用重写的 ToString("dd/mm/yyyy")
  • 使用DataFormatString属性(如果使用BoundField
  • "数据库中存储的数据是 2015-07-02 17:15:47.000" 不,不是。它以 DateTime 的格式返回,并且在 Sql Management Studio 中显示 DateTime 对象的默认方式是以您看到的方式显示它。

标签: c# sql asp.net gridview


【解决方案1】:
select * from table_name
where convert(varchar,column_name,103)='02/07/2015'

查看此链接了解任何类型的日期转换:-

http://www.w3schools.com/sql/func_convert.asp

【讨论】:

  • 这个查询会出错。将 varchar 值“Value”转换为数据类型 int 时转换失败。
  • 您的查询应该是这样的:- select * from table_name where convert(varchar,column_name,103)='02/07/2015' 这不是用户提出的问题。它只会检查日期的条件,不会将日期转换为 dd/MM/yyyy。
【解决方案2】:
select convert(varchar,YourDateColumn,103) from YourTable

所有类型的日期转换请访问Here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-06
    • 2014-03-02
    • 1970-01-01
    • 2017-10-16
    • 2015-04-23
    • 1970-01-01
    相关资源
    最近更新 更多