日期格式字符串比较不会工作? [英] string comparison on date format wont work?

查看:109
本文介绍了日期格式字符串比较不会工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

喜出于某种原因,我不能做的日期字符串比较?举个例子:

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;
}



它只是不会工作?如果我输入到一个文本框 13/07/2012 这是如何将其存放它返回一个404没有找到???

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

输出看起来像这样从一个普通的列表/ GET请求:

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>

有另一种方式来找到它正斜杠的字符串? ?例如使用/在任何网页字符串comparers不起作用它总会抛出一个404没有找到

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)将字符串在另一个字符串中,要做到这一点最常见的方法是使用的 的String.Format 。该方法采用格式字符串(如日期:{0}时间:{1})和一堆参数。 每出现一次{0} 在字符串中的第一个参数, {1} 由第二等取代。有其他选项来格式化字符串中的参数,更多信息请参见的在MSDN页面上的String.Format

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)如果你有一个网址而在应用程序中得到404,首先验证URL的语法是正确的。尝试手动你在你的程序在浏览器中直接创建的URL,如果它不工作,找出语法实际上是用来提供参数。例如,它可能是一个日期的格式必须为 13-07-2012 而不是 13/07/2012 为它工作。如果是的话,你也许可以通过选择合适的 的CultureInfo解决这个

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.

有关的任​​何 DateTime的日期对象,来格式化它有使用任何的CultureInfo 你想,使用的ToString 的过载,并指定 D <短日期/ code>作为格式。例如,使用不变的文化:

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);



其它格式字符串可以发现的这里

这篇关于日期格式字符串比较不会工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆