Reputation: 53
How to get only the date excluding time in asp.net c#. I want only the date to be given as input to search like eg 3/11/2013
Upvotes: 5
Views: 32041
Reputation: 73
if you are brining it back from the Database though MVC then use ToShortDateString()
.
Example for a company registration date:
txtComRegDate.Text = Convert.ToString(blCompanyMaster.ComRegDate.ToShortDateString());
Here ComRegDate
where the data comes form Business Layer.
Upvotes: 0
Reputation: 63
I saved the database as datetime in MS-SQL 2014, but when i need to only in date i do as 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 contain List of Model (IEnumerable)... I Solve this way. Thank you.
Upvotes: 1
Reputation: 1845
try to use this:
@{
DateTime dd=DateTiem.Now;
string date=dd.toString("dd/MM/yyyy");
}
Now to view just:
@date
Upvotes: 3
Reputation: 148180
You can use DateTime.Date to get only date part of DateTime object
DateTime dateOnly = date1.Date;
A new object with the same date as this instance, and the time value set to 12:00:00 midnight (00:00:00).
If you have the Date in string and want to convert it to DateTime object first then you can use DateTime.ParseExact
result = DateTime.ParseExact("3/11/2013", "d/MM/yyyy", CultureInfo.InvariantCulture);
Upvotes: 9
Reputation: 17194
DateTime dt = your_Dt.Date;
OR
You can format it into whatever format you want as below:
dt.Tostring("MM/dd/yyyy");
OR
You can convert the valued to shortdate as:
Convert.ToDateTime(your_Dt).ToShortDateString();
Upvotes: 2