我是编程新手。运营商无法应用
var time = DateTime.Now.ToShortTimeString().ToString();
var timePattern = "09:30";
if (time.ToString() <= timePattern.ToString())
{
//disable the button
}
错误显示:操作员“< =”不能应用于类型“串”和“串”的操作数
任何人可以帮助我写这行代码时,我得到的错误?
我是编程新手。运营商无法应用
var time = DateTime.Now.ToShortTimeString().ToString();
var timePattern = "09:30";
if (time.ToString() <= timePattern.ToString())
{
//disable the button
}
错误显示:操作员“< =”不能应用于类型“串”和“串”的操作数
任何人可以帮助我写这行代码时,我得到的错误?
您不能应用小于等于(<=
)的运算符来键入string
。
看起来你正试图检查当前时间是否小于9:30。为此,请比较DateTime
实例。
DateTime currentTime = DateTime.Now;
//Creates a DateTime instance with the current year, month, day at 9:30AM
DateTime nineThirty =
new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, 9, 30, 0);
if(currentTime.TimeOfDay <= nineThirty.TimeOfDay)
{
//your code
}
< =运算符没有为字符串的值定义。 而应该让你在比较DateTime的情况而言
看看这个:http://msdn.microsoft.com/en-us/library/system.datetime.compare.aspx
不要把DateTime是否为字符串对它们进行比较,直接使用DateTime是否。
为了把字符串转换成一个DateTime,使用DateTime.Parse或DateTime.ParseExact
注意
比较字符串:
使用String.Compare
到compare字符串这样。
<=
尚未实施字符串。
您应该直接比较DateTime
s,而不是将它们转换为字符串。该<=
operator has been implemented为DateTime
所以它应该是那么容易,因为:
var time = DateTime.Now;
var timePattern = new DateTime(time.Year, time.Month, time.Day, 9, 30, 0);
if (time <= timePattern)
{
//disable the button
}
仅供参考,你不能字符串使用<=
,你将需要使用string.CompareTo
代替。
if (time.ToString().CompareTo(timeParrent.ToString()) <= 0)
还是static
方法string.Compare
一种替代语法。
if (string.Compare(time.ToString(), timeParrent.ToString()) <= 0)
而且DateTime.ToShortTimeString()
不会放弃的排序(在所有情况下)格式的格式。您可以使用time.ToString("u")使用可排序的日期/时间模式格式将日期作为字符串获取。一个你想要做的例子就是将日期打印到HTML中并用JavaScript对它进行排序。
你可以做到这一点,而无需指定年/月/日...
if (DateTime.Now.TimeOfDay < new TimeSpan(9, 30, 0))
{
// ... it's before 9:30 am ...
}
使用''==而不是'<=' – Xaqron 2013-05-02 04:20:51
okay..i'll尝试另一种选择1号..感谢 – azfar 2013-05-02 04:29:21