This code gets the data 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 RelUse
        {
            public DateTime Date { get; set; }
        }
        public RelUse()
        {
            atribuiValores(row);
        }
        public RelUse(DataRow row)
        {
            assignValues(row);
        }
        protected void assignValues(DataRow row)
        {
            this.Date = 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

Dani AI

Generated

A DateTime value does not carry a display format. It stores a point in time. Formatting only matters when you convert it to a string for output. tried calling toString (C# is case sensitive) and did not use the returned string; that won’t change the DateTime value. was correct to suggest using ToString, but remember format tokens matter: MM = months, mm = minutes; HH = 24‑hour, hh = 12‑hour (use tt for AM/PM).

A couple of safe options you can apply immediately:

  • Add a read‑only string property on the model so the UI can bind to a formatted value:
public string DateFormatted
{
    get { return Date.ToString("dd'/'MM'/'yyyy HH':'mm':'ss", System.Globalization.CultureInfo.InvariantCulture); }
}
  • Format in ASP.NET binding instead of changing the model:
<asp:BoundField DataField="Date" DataFormatString="{0:dd'/'MM'/'yyyy HH':'mm':'ss}" />

Troubleshooting notes: check for DBNull before converting row values to DateTime; ToString returns a new string — it does not mutate the DateTime; do not store formatted strings in the database (store DateTime and format at display time). If you need a culture‑independent literal slash, escape it as shown above (the / placeholder otherwise becomes the current culture’s date separator). If round‑trips are required, use DateTime.ParseExact with the same format.

Recommended Answers

All 3 Replies

Hi

When outputting the value of the DateTime you can use the ToString method and pass a format string. For example:

Console.WriteLine(Date.ToString("dd/MM/yyyy hh:mm:ss"));

HTH

I put:

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 RelUse
        {
            public DateTime Date { get; set; }
        }
        public RelUse()
        {
            atribuiValores(row);
        }
        public RelUse(DataRow row)
        {
            assignValues(row);
        }
        protected void assignValues(DataRow row)
        {
            this.Date = Convert.ToDateTime(row["Date"]);
            Date.toString("dd/MM/yyyy hh:mm:ss")
        }
    } 

And nothing has changed

Hi

Nothing will change until you actually use the Date. So for example, if you want to display the date in a control then you would use the code that I posted.

Can you provide some more info on what it is you want to do with the Date.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.