简体   繁体   中英

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

This code gets the date from the table to show in a table for the user

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"]);
    }
}    

It gets the date how it is in the database yyyy-mm-dd hh:mm:ss. Can I do some method in the DateTime to change the formatting of the date to dd/mm/yyyy hh:mm:ss

You can convert the DateTime structure to string using the following code:

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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