Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
string date comparsion using linq C#

I got error "String was not recognized as a valid DateTime"

C#
string ctfromdate="20/10/2015";
string cttodate="20/10/2015";

var startDate = DateTime.Parse(ctfromdate);
var EndDate = DateTime.Parse(cttodate);


Linq code:

C#
   var date = item.ctroomlist.Where(x => x.RoomTypeId == RoomTypeId).SingleOrDefault().ctroomrate.
Where(x => Convert.ToDateTime(x.Date)>=startDate &&Convert.ToDateTime(x.Date)<= EndDate).ToList();
Posted
Updated 5-Oct-15 4:06am
v4
Comments
Member 10918596 5-Oct-15 9:59am    
var date = item.ctroomlist
.Where(x => x.RoomTypeId == RoomTypeId).SingleOrDefault().ctroomrate
.Where(x =>
Convert.ToDateTime(x.Date) >= startDate &&
Convert.ToDateTime(x.Date) <= EndDate)
.ToList();
Richard Deeming 5-Oct-15 11:29am    
Simple answer: don't store dates as strings. In .NET, use the DateTime type. In your database, use one of the available date types.

Storing dates as strings will only lead to pain later on.

Try this,
var fromDate = DateTime.ParseExact(ctfromdate, "dd/MM/yyyy", null);

-KR
 
Share this answer
 
Hmmm...
In few words: string representation of DateTime data type depends on localization.

Please see:
Design and Implementation Guidelines for Web Clients[^]
How to: Display Localized Date and Time Information to Web Users[^]
Formatting Date and Time for a Specific Culture[^]

I'd suggest to use extension method to "convert" string into DateTime using specific localization:
C#
public static class DateExtensionMethods
{
	public static DateTime StrToDate(string strDate)
	{	
		System.Globalization.CultureInfo culture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US"); //en-US //fr-FR //de-DE
		DateTime dt;
		//if convertion failed, return default value 
		return DateTime.TryParse(strDate, culture, System.Globalization.DateTimeStyles.None, out dt) ? dt : new DateTime(1899,12,31);
	}
}


Usage:
C#
void Main()
{
    List<string> stringDates = new List<string>()
        {"20/10/2015",
        "10/20/2015",
        "2013/10/20",
        "10/20/2014",
        "2015/10/20",
        "2015/20/10"};

    var result = stringDates.Select(d=>DateExtensionMethods.StrToDate(d));

}


Result:
1899-12-31 00:00:00
2015-10-20 00:00:00
2013-10-20 00:00:00
2014-10-20 00:00:00
2015-10-20 00:00:00
1899-12-31 00:00:00


Final remark:
you should work on data, not on its string representation!
 
Share this answer
 
C#
var date = item.ctroomlist.Where(x => x.RoomTypeId == RoomTypeId).SingleOrDefault().ctroomrate.AsEnumerable().Where(x => DateTime.Parse(x.Date)>

you miss some code in your question but the above should do!
 
Share this answer
 
v3
Comments
Member 10918596 5-Oct-15 10:02am    
var date = item.ctroomlist
.Where(x => x.RoomTypeId == RoomTypeId).SingleOrDefault().ctroomrate
.Where(x =>
Convert.ToDateTime(x.Date) >= startDate &&
Convert.ToDateTime(x.Date) <= EndDate)
.ToList();

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