Hossein Tavakoli
Hossein Tavakoli

Reputation: 123

get date without time in c#

i have a column on my table "AbsenteeismDate" (type=date) when i want to get rows contain a date it return 0 row.

this is my C# code :

DateTime ClassDate = DateTime.Parse(lblDate.Content.ToString());
var Abs = dbs.GETAbsenteeisms.Where(a => a.AbsenteeismDate == ClassDate ).ToList();

i checked it there is a problem : "AbsenteeismDate" on database and "ClassDate" aren't equal.

eg.

AbsenteeismDate=1396-05-31

and

ClassDate=1396-05-31 12:00:00 AM

how can i get Date without Time with DateTime type because AbsenteeismDate's type is date on my database.

sorry i can't speak English very well.

Upvotes: 0

Views: 10080

Answers (5)

Rahul mishra
Rahul mishra

Reputation: 82

You can try like this byt setting the date format:

DateTime ClassDate = DateTime.Parse(lblDate.Content.ToString(("yyyy-MM-dd"));
var Abs = dbs.GETAbsenteeisms.Where(a => a.AbsenteeismDate == ClassDate ).ToList();

Upvotes: 0

NotoriousM
NotoriousM

Reputation: 1

This works for me:

string a = DateTime.Now.ToShortDateString();

ToShortDateString() converts the value of the current System.DateTime object to equivalent short date string representation. This means the Console output woulf be e.g. 01.01.2017

Upvotes: 0

Tom
Tom

Reputation: 15151

You do just define date time but without the time:

string date = System.DateTime.Now.ToString("yyyy-MM-dd");

Upvotes: 3

Malitha Shan
Malitha Shan

Reputation: 21

Use the Date property:

DateTime ClassDate = DateTime.Parse(lblDate.Content.ToString());
var date = ClassDate.Date;

Otherwise you will have to convert to string as follow

var date=ClassDate.ToString("yyyy-MM-dd");

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460258

A DateTime always has a date and time portion, but if you want to get a DateTime of that date and the time value set to 12:00:00 midnight (00:00:00) use DateTime.Date:

var Abs = dbs.GETAbsenteeisms
   .Where(a => a.AbsenteeismDate == ClassDate.Date)
   .ToList();

Upvotes: 7

Related Questions