Select Date from DateTime database filed using Entity Framework in ASP.Net MVC

alya14
 
on Dec 08, 2020 06:54 AM
2652 Views

hi;

I have Datetime field and its stored to table like 2020-12-08 15:14:45

When I call like this

var tbl=db.Customer.First(); 

tbl.SENDATE = Convert.ToDateTime(tbl.SENDATE.ToString("yyyy-mm-DD")),

the result not like 2020-12-08 

      DataSourceResult result = data.ToDataSourceResult(request, x => new EntityModel
      {
        DURUMU = x.DURUMU,
        DEFINITION_ = x.DEFINITION_,
        TAXNR = x.TAXNR,
        SENDATE = Convert.ToDateTime(x.SENDATE.ToString("yyyy-MM-dd")),
        RESPONDATE = Convert.ToDateTime(Convert.ToDateTime(x.RESPONDATE).ToString("yyyy-MM-dd")),
        SENDERINFO = x.SENDERINFO,
        RESPONDINFO = x.RESPONDINFO,
        INVAMOUNT = x.INVAMOUNT,
        PRICE = x.PRICE,
        APPROVER = x.APPROVER
 
      });
      return Json(result);

its showing 11.12.2020

this is shows: 2020-12-07 so good.

var test = DateTime.ParseExact(data.SENDATE.ToString("dd.MM.yyyy hh:mm:ss"), "dd.MM.yyyy hh:mm:ss",
          CultureInfo.InvariantCulture).ToString("yyyy-MM-dd");

 this is shows : 17.12.2020 00:00:00

DateTime test2 = Convert.ToDateTime(test);

I think the problem occur after convert. I want to escape hh:mm:ss

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Dec 11, 2020 11:08 PM
alya14 says:
DateTime test2 = Convert.ToDateTime(test);

When you convert to DateTime it will display with time.

If time is not specified then it will show as 00:00:00.

If you want to remove the time then you need to convert to string using .ToString() method and save in string variable.

string test2 = (Convert.ToDateTime(test))ToString("yyyy-MM-dd");