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
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#]
2 Comments
Wajahat
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.
Wajahat
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