string comparison on date format wont work?

若如初见. 提交于 2019-12-24 20:36:45

问题


Hi for some reason I cant do a string comparison on a date? Take for example:

public List<HireDate> GetHireDate(string anything)
{
List<HireDate> hiredate = hiredates.Where(n =>
string.Equals(n.HireFromDate, anything, StringComparison.CurrentCultureIgnoreCase)
).ToList();
return hiredate;
}

It simply wont work? if I type into a textbox 13/07/2012 which is how its stored it returns a 404 not found???

The output looks like this from a generic list/get request:

<ArrayOfHireDate>
<HireDate>
<HireFromDate>13/07/2012</HireFromDate>
<HireToDate>28/07/2012</HireToDate>
<NumberOfDaysHired>15</NumberOfDaysHired>
</HireDate>
</ArrayOfHireDate>

Is there another way to find a string with a forward slash in it? For instance using / in any of web string comparers does not work it will always throw a 404 not found?


回答1:


Two things:

1) To put a string in another string, the most common way to do this is using String.Format. That method takes a format string (such as "Date: {0} Time: {1}") and a bunch of arguments. Each occurrence of {0} in the string is replaced by the first argument, {1} by the second, etc.. There are additional options to format the arguments in the string, see for more information the MSDN page on String.Format.

2) If you have an URL and you get a 404 in your application, first verify that the syntax of the URL is correct. Manually try the URL you create in your program directly in your browser, and if it does not work, find out what syntax is actually used to provide the arguments. For example, it might be that a date must be formatted as 13-07-2012 instead of 13/07/2012 for it to work. If so, you can probably solve this by choosing the appropriate CultureInfo.

For any DateTime date object, to format it has a short date using any CultureInfo you want, use an overload of ToString and specify d as the format. For example, using the invariant culture:

var str = date.ToString("d", CultureInfo.InvariantCulture);

Other format strings can be found here.



来源:https://stackoverflow.com/questions/11660423/string-comparison-on-date-format-wont-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!