设置字段DateTime以dd / mm / yyyy hh:mm:ss格式显示日期和时间

时间:2015-01-07 16:20:26

标签: c# sql-server date datetime formatting

此代码从表中获取日期以显示在用户的表中

using System; 
using System.Data; 
using System.Configuration; 
using System.Web; 
using System.Web.Security; 
using System.Collections; 
using System.Collections.Generic;
using System.Text;

namespace Form 
{
    public class RelUso
    {
        public DateTime Date { get; set; }
    }
    public RelUso()
    {
        atribuiValores(row);
    }
    public RelUso(DataRow row)
    {
        assignValues(row);
    }
    protected void assignValues(DataRow row)
    {
        this.Data = Convert.ToDateTime(row["Date"]);
    }
}    

它获取数据库中的日期yyyy-mm-dd hh:mm:ss。我可以在DateTime中使用某种方法将日期格式更改为dd / mm / yyyy hh:mm:ss

2 个答案:

答案 0 :(得分:4)

您可以使用以下代码将DateTime结构转换为字符串:

var dt = DateTime.Now;
string str = dt.ToString("yyyy-MM-dd hh:mm:ss"); //(For month use caps 'MM')

答案 1 :(得分:2)

使用

Convert.ToDateTime(row["Date"]).ToString("dd/MM/yyyy HH:mm:ss")