0

我正在使用下面的代码来比较两个时间:

DateTime systemtime = DateTime.Now;
DateTime timestart = Convert.ToDateTime(txtTimestart.Text);
DateTime timeend = Convert.ToDateTime(txtTimeend.Text);

if (systemtime < timestart || systemtime > timeend)
{
    MessageBox.Show("not auth.");
}
else
{
    MessageBox.Show("auth.");
}

但我也想比较当天。例如,如果今天是星期一或星期四,则用户已通过身份验证,否则不会。我该怎么做?谢谢。

4

4 回答 4

1
DateTime systemtime = DateTime.Now;
if(systemtime.DayOfWeek == DayOfWeek.Monday)
{
...
}
于 2012-07-20T00:23:31.797 回答
1

timestart.DayOfWeek会给你诸如DayOfWeek.Thursday或之类的值DayOfWeek.Friday

有关文档和示例,请参见此处:http: //msdn.microsoft.com/en-us/library/system.datetime.dayofweek.aspx

于 2012-07-20T00:23:50.423 回答
0

DateTime对象将具有一个属性DayOfWeek。你可以只比较字符串。

于 2012-07-20T00:23:11.233 回答
0

试试这个

var systemtime = DateTime.Now;
var start = "Monday";
var finish = "Wednesday";
DayOfWeek startDay;
if (!Enum.TryParse<DayOfWeek>(start ,out startDay))
{
    //handle parse error 
}
DayOfWeek finishDay;
if (!Enum.TryParse<DayOfWeek>(finish, out finishDay))
{
    //handle parse error
}

if (systemtime.DayOfWeek < startDay || systemtime.DayOfWeek > finishDay)
{
    MessageBox.Show("not auth.");
}
else
{
    MessageBox.Show("auth.");
}
于 2012-07-20T07:02:38.573 回答