How to get only date excluding time in asp.net c #

How to get only date excluding time in asp.net c #. I want only the date as the input for the search, for example, 3/11/2013

+5
source share
4 answers

You can use DateTime.Date to get only the date in a DateTime object

DateTime dateOnly = date1.Date;

A new object with the same date as this instance and the time value set for 12:00:00 at midnight (00:00:00).

If you have a date in a string and you want to convert it to a DateTime object first, you can use DateTime.ParseExact

result = DateTime.ParseExact("3/11/2013", "d/MM/yyyy", CultureInfo.InvariantCulture);
+8
source

try using this:

  @{
     DateTime dd=DateTiem.Now;
     string date=dd.toString("dd/MM/yyyy");
   }

Now just:

  @date
+2
source
DateTime dt = your_Dt.Date;

, , :

dt.Tostring("MM/dd/yyyy");

shortdate :

Convert.ToDateTime(your_Dt).ToShortDateString();
+1

I saved the database as a date-time in MS-SQL 2014, but when I need only the date, I do as shown below .... in cshtml

@foreach (var item in Model.dbModelLst)
{
            <tr>
              <td>@item.ChargeFrequency</td>
              <td>@item.Charge</td>
              <td>@item.FromDate.ToShortDateString().ToString()</td>
              <td>@item.ToDate.ToShortDateString().ToString()</td>
              <td>@item.Inv_SerialNo.SerialNo</td>
              <td>@item.IncludeLic</td>
              <td>@item.Status</td>
               .....
               .....
           </tr>
}

Where dbModelist contains the Model List (IEnumerable) ... I solve this way. Thank.

+1
source

All Articles