Manish Tiwari Manish Tiwari - 4 months ago 56
SQL Question

How to get only date from date time using Asp. Net c#?

In sql database, value is in this format

2016-07-16
and it's datatype is
date
.

But when i am getting it gives me like that :
7/16/16 12:00:00 AM


I want to get only the date
2016-07-16
but with this format
16/07/2016
.

How to get it?

Answer

When you fetch the data from the sql you should manipulate the data and change this value. i don't know if your using linq or using EF.

if you use linq

var query = from p in db.table
            select new view{
               CreatedOn = p.CreatedOn.ToString("dd/MM/yyyy")
            };

but if you use EF and code first you can set the GET method on its property like this :

private datetime _createdOn;

public datetime CreatedOn {

    get {
         return _createdOn.ToString("dd/MM/yyyy");
    }
    set{
        _createdOn = value;
    }

}
Comments