0

I have a column "create date" in the database table of the type DateTime, but my problem is that I want to show the retrieved datetime value in a particulat date format like "dd/mm/yyyy" how can I do that?

1 Answer 1

1

You can show a DateTime variable in diffrent formats, e.g if your variable is dt you can show it in dd/mm/yyyy Format by following command.

string.Format("{0:dd/MM/yyyy}", dt);          // "03/09/2008"

You can use it as following:

public class Project
{
   ...
   public DateTime CreationDate { get; set; }
   public string FormattedCreationDate
   {
      get
      {
         return string.Format("{0:dd/MM/yyyy}", CreationDate);
      }
   }
}

You can find some more formats at String Format for DateTime [C#]

Sign up to request clarification or add additional context in comments.

2 Comments

I am doing something like this. public List<Project> GetProjects() { var projects = new List<Project>(); using (var database = new ProjectDatabase()) { projects = database.Context.Projects.ToList(); } return projects; } One column of project entity is of type datetime. Now I want all the rows returned by this function return the datetime column as per my required date format.
Yeah something like that. Created a partial class of the same name as generated by EF and put almost same code that you have provided but in the GetProject function. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.