繁体   English   中英

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

[英]Set field DateTime to show the date and time in the format dd/mm/yyyy hh:mm:ss

此代码从表格中获取日期以显示给用户

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做一些方法来将DateTime格式更改为dd / mm / yyyy hh:mm:ss

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

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

采用

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM