Reputation: 16272
i was looking for date compare in LINQ and i found a code from searching google.
var currentDate = DateTime.Now.Date;
VisitorLog log = db.Context.VisitorLogs .Where(vl=>vl.inDate.Date == currentDate).FirstOrDefault();
i need to know the above code works fine or not.
how do i compare date in particular format like i compare date in sql server
compare(varchar(10),mydatefield,112)>=compare(varchar(10),@mydatefield,112)
so please guide me how could i compare date using linq in particular format like above one.
thanks
Upvotes: 2
Views: 20898
Reputation: 93000
The format matters when you parse a string
to a DateTime
object. Once you have done that and you have two DateTime
objects then comparing their Date
properties is the correct thing to do. The Date
properties return DateTime
objects with time set to 12:00am
In order to parse the date correctly you can pass a IFormatProvider
to the DateTime.Parse()
method. E.g. a CultureInfo
object:
DateTime d = DateTime.Parse("07/04/2011", CultureInfo.GetCultureInfo("en-us"));
Upvotes: 0
Reputation: 6890
The thing is that you will rarely compare dates in a specific format(unless you want to compare them as strings or something). Thus is the introduction of the DateTime structure.
Anyway the most used way of comparing dates is not to check for equality but to actually check whether a date comes into a specific range. For example:
startDate <= order.OrderDate && order.OrderDate < endDate
Upvotes: 3
Reputation: 842
Can this link help you?
where t.CreateDate.Date < DateTime.Today.AddMonths(-1)
Upvotes: 1