0

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?

Manish Tiwari
  • 1,806
  • 10
  • 42
  • 65
  • Possible duplicate of [How to return the date part only from a SQL Server datetime datatype](http://stackoverflow.com/questions/113045/how-to-return-the-date-part-only-from-a-sql-server-datetime-datatype) – Vijay Kumbhoje Jul 17 '16 at 04:53

3 Answers3

0

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 string CreatedOn {

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

}
M.R.Safari
  • 1,857
  • 3
  • 30
  • 47
0

public string GetDateOnly(string DateString) {

    DateTime _retVal = Convert.ToDateTime(DateString);
    return _retVal.ToString('dd/MM/yyyy');

}

this this method where by passing datetime string

0

I had the same problem. This is what I did following @M.R.Safari using the Entity Framework migrations and code first approach.

    [Required]
    public DateTime StartDate { get; set; }
    public string StartDateShort
    {
        get
        {
            return StartDate.ToString("dd/MM/yyyy");
        }
        set
        {
            StartDateShort = value;
        }
    }
    [Required]
    public DateTime EndDate { get; set; }
    public string EndDateShort
    {
        get
        {
            return EndDate.ToString("dd/MM/yyyy");
        }
        set
        {
            EndDateShort = value;
        }
    }

I did get an error on the Set method using @M.R.Safari example since it was trying to overwrite the StartDate(DateTime) value to a String, so I instead used the Set method for the String version. I edit only the StartDate(DateTime) value in my forms and the string value gets updated automatically. I use the string value for views.