Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have create model class with name invoice, and one column is that date.
public class Invoice
{
public string date{get;set;}
}

controller code is:

List<invoice> lst = db.Invoices.Where(u => u.Date.CompareTo(to) <= 0 && u.Date.CompareTo(frm)>=0).ToList();

when i want data between two month. lst showing all the data from another month also.
also suggest me how to compare two date of datatype string?
Posted

1 solution

Simple: don't.

Never try to compare dates as strings - there are far, far too many ways it can go wrong.
Always convert dates from string to DateTime as soon as possible after the user input them, and convert them back to strings as late as possible before you present them back to the user. If you don't there are just too many ways that everything can go wrong. For example, if you have two users entering dates in different formats, unless you know what format each used (and you can only get that when the input is done) you can't safely compare them at all:
01/02/03 could be 1st Feb 2003, 2nd Jan 2003, or 3rd Feb 2001 depending on the nationality of your user. A different user could enter 02/01/03 for exactly the same date...and if you store and manipulate your dates as strings you don't know which it is!
 
Share this answer
 
Comments
Zaheer A.M. 12-Sep-14 2:25am    
Sir,Actually i'm using jquery date picker (dateFormat: 'dd-mm-yy') to store date in database.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900