如何验证所选日期是星期一 [英] how to validate selected date is monday
问题描述
我的代码如下
DateTime selecteddate = Txtfromdate.SelectedDateValue.ToString();
if(selecteddate.DayOfWeek!= DayOfWeek .Monday)
{
lblmsg.Text =哟必须只选择星期一日期;
返回;
$
当执行上面的代码时显示错误如下
不能隐式地将类型'string'转换为'System.DateTime'
以上错误在下面的行显示如下
DateTime selecteddate = Txtfromdate.SelectedDateValue.ToString();
请帮帮我。
My code as follows
DateTime selecteddate = Txtfromdate.SelectedDateValue.ToString();
if (selecteddate.DayOfWeek != DayOfWeek.Monday)
{
lblmsg.Text = "Yo must select the monday date only";
return;
}
wheni execute the above code shows error as follows
Cannot implicitly convert type 'string' to 'System.DateTime'
The above error shows in below line as follows
DateTime selecteddate = Txtfromdate.SelectedDateValue.ToString();
please help me.
推荐答案
好吧,不要不要尝试将字符串分配给日期时间变量!这样的事情出了什么问题:
Well, than don't try to assing a string to a datetime variable! What's wrong with something like this:
if (Txtfromdate.SelectedDateValue.DayOfWeek != DayOfWeek.Monday)
{
lblmsg.Text = "Yo must select the monday date only";
return;
}
假设 SelectedDateValue
字段的类型为 DateTime
,因为不清楚什么类型的对象 Txtfromdate
是。
Assuming that SelectedDateValue
field is of type DateTime
, since it is not clear what kind of object Txtfromdate
is.
不要使用ToString(),使用
Txtfromdate.SelectedDate.ToShortDateString()
Dont use ToString(), Use
Txtfromdate.SelectedDate.ToShortDateString()
这篇关于如何验证所选日期是星期一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!