Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

asp.net

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

like image 327
Shodhan Huli Avatar asked Mar 11 '13 10:03

Shodhan Huli


3 Answers

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);
like image 192
Adil Avatar answered Oct 06 '22 22:10

Adil


try to use this:

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

Now to view just:

  @date
like image 38
SAR Avatar answered Oct 06 '22 21:10

SAR


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();
like image 1
Vishal Suthar Avatar answered Oct 06 '22 21:10

Vishal Suthar