例如
08:30 是否在 08:00-20:00
8:30 是否在 20:00-08:00
9 个解决方案
#1
(new TimeSpan(8,0,0))<DateTime.TimeOfDay<(new TimeSpan(20,0,0))
#2
先转换成Int32,再用代码逐个判断
#3
string date1 = maskedTextBox1.Text;
DateTime d1 = Convert.ToDateTime(date1);
if (d1 < dateTimePicker1.MinDate)
{
MessageBox.Show("请输入在1753-1-1到9998-12-31之间的时间");
label1.Text = "";
maskedTextBox1.Text = "";
return;
}
else if (d1 > dateTimePicker1.MaxDate)
{
MessageBox.Show("请输入在1753-1-1到9998-12-31之间的时间");
label1.Text = "";
maskedTextBox1.Text = "";
return;
}
DateTime d1 = Convert.ToDateTime(date1);
if (d1 < dateTimePicker1.MinDate)
{
MessageBox.Show("请输入在1753-1-1到9998-12-31之间的时间");
label1.Text = "";
maskedTextBox1.Text = "";
return;
}
else if (d1 > dateTimePicker1.MaxDate)
{
MessageBox.Show("请输入在1753-1-1到9998-12-31之间的时间");
label1.Text = "";
maskedTextBox1.Text = "";
return;
}
#4
,,,
#5
也可以用DateTime类型直接判断,但是“8:30 是否在 20:00-08:00”这个要加上完整的日期,再判断
#6
上面的方案都不行,我的时间范围是任何可以给
比如
07:20-16:00
或者
20:30-07:00
比如
07:20-16:00
或者
20:30-07:00
#7
最后用了一个笨拙的办法解决了
代码如下
public static bool TimeScope(string begintime, string endtime, string timestr)
{
DateTime date = DateTime.Now;
DateTime stime = Convert.ToDateTime(ConvertToDataString(date) + " " + begintime);
DateTime etime = Convert.ToDateTime(ConvertToDataString(date) + " " + endtime);
DateTime now = Convert.ToDateTime(ConvertToDataString(date) + " " + timestr);
decimal start = Convert.ToDecimal(stime.Hour) + Convert.ToDecimal(stime.Minute) / 100;
decimal end = Convert.ToDecimal(etime.Hour) + Convert.ToDecimal(etime.Minute) / 100;
decimal nowtime = Convert.ToDecimal(now.Hour) + Convert.ToDecimal(now.Minute) / 100;
if (end < start)
{
if (nowtime >= start || nowtime <= end)
{
return true;
}
else
{
return false;
}
}
else
{
if (nowtime >= start && nowtime <= end)
{
return true;
}
else
{
return false;
}
}
}
代码如下
public static bool TimeScope(string begintime, string endtime, string timestr)
{
DateTime date = DateTime.Now;
DateTime stime = Convert.ToDateTime(ConvertToDataString(date) + " " + begintime);
DateTime etime = Convert.ToDateTime(ConvertToDataString(date) + " " + endtime);
DateTime now = Convert.ToDateTime(ConvertToDataString(date) + " " + timestr);
decimal start = Convert.ToDecimal(stime.Hour) + Convert.ToDecimal(stime.Minute) / 100;
decimal end = Convert.ToDecimal(etime.Hour) + Convert.ToDecimal(etime.Minute) / 100;
decimal nowtime = Convert.ToDecimal(now.Hour) + Convert.ToDecimal(now.Minute) / 100;
if (end < start)
{
if (nowtime >= start || nowtime <= end)
{
return true;
}
else
{
return false;
}
}
else
{
if (nowtime >= start && nowtime <= end)
{
return true;
}
else
{
return false;
}
}
}
#8
用TimeSpan+DateTime结合,很容易实现你的想法:
DateTime curr = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 8, 30, 0);//填实际值
DateTime t1 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 8, 0, 0);
DateTime t2 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 20, 0, 0);
TimeSpan span1 = t1.Subtract(curr);
TimeSpan span2 = t2.Subtract(curr);
if(span1.TotalSeconds<=0 && span2.TotalSeconds>=0)
{
//落在区间范围
}
DateTime curr = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 8, 30, 0);//填实际值
DateTime t1 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 8, 0, 0);
DateTime t2 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 20, 0, 0);
TimeSpan span1 = t1.Subtract(curr);
TimeSpan span2 = t2.Subtract(curr);
if(span1.TotalSeconds<=0 && span2.TotalSeconds>=0)
{
//落在区间范围
}
#9
DateTime dt1 = new DateTime(DateTime.Now.Year, DateTime.Now.Month,DateTime.Now.Day,9,0,0);
DateTime dt2 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 18, 0, 0);
TimeSpan ts1 = dt1.Subtract(DateTime.Now);
TimeSpan ts2 = dt2.Subtract(DateTime.Now);
if (ts1.TotalSeconds < 0 && ts2.TotalSeconds > 1)
{
Response.Write("OK");
}
#1
(new TimeSpan(8,0,0))<DateTime.TimeOfDay<(new TimeSpan(20,0,0))
#2
先转换成Int32,再用代码逐个判断
#3
string date1 = maskedTextBox1.Text;
DateTime d1 = Convert.ToDateTime(date1);
if (d1 < dateTimePicker1.MinDate)
{
MessageBox.Show("请输入在1753-1-1到9998-12-31之间的时间");
label1.Text = "";
maskedTextBox1.Text = "";
return;
}
else if (d1 > dateTimePicker1.MaxDate)
{
MessageBox.Show("请输入在1753-1-1到9998-12-31之间的时间");
label1.Text = "";
maskedTextBox1.Text = "";
return;
}
DateTime d1 = Convert.ToDateTime(date1);
if (d1 < dateTimePicker1.MinDate)
{
MessageBox.Show("请输入在1753-1-1到9998-12-31之间的时间");
label1.Text = "";
maskedTextBox1.Text = "";
return;
}
else if (d1 > dateTimePicker1.MaxDate)
{
MessageBox.Show("请输入在1753-1-1到9998-12-31之间的时间");
label1.Text = "";
maskedTextBox1.Text = "";
return;
}
#4
,,,
#5
也可以用DateTime类型直接判断,但是“8:30 是否在 20:00-08:00”这个要加上完整的日期,再判断
#6
上面的方案都不行,我的时间范围是任何可以给
比如
07:20-16:00
或者
20:30-07:00
比如
07:20-16:00
或者
20:30-07:00
#7
最后用了一个笨拙的办法解决了
代码如下
public static bool TimeScope(string begintime, string endtime, string timestr)
{
DateTime date = DateTime.Now;
DateTime stime = Convert.ToDateTime(ConvertToDataString(date) + " " + begintime);
DateTime etime = Convert.ToDateTime(ConvertToDataString(date) + " " + endtime);
DateTime now = Convert.ToDateTime(ConvertToDataString(date) + " " + timestr);
decimal start = Convert.ToDecimal(stime.Hour) + Convert.ToDecimal(stime.Minute) / 100;
decimal end = Convert.ToDecimal(etime.Hour) + Convert.ToDecimal(etime.Minute) / 100;
decimal nowtime = Convert.ToDecimal(now.Hour) + Convert.ToDecimal(now.Minute) / 100;
if (end < start)
{
if (nowtime >= start || nowtime <= end)
{
return true;
}
else
{
return false;
}
}
else
{
if (nowtime >= start && nowtime <= end)
{
return true;
}
else
{
return false;
}
}
}
代码如下
public static bool TimeScope(string begintime, string endtime, string timestr)
{
DateTime date = DateTime.Now;
DateTime stime = Convert.ToDateTime(ConvertToDataString(date) + " " + begintime);
DateTime etime = Convert.ToDateTime(ConvertToDataString(date) + " " + endtime);
DateTime now = Convert.ToDateTime(ConvertToDataString(date) + " " + timestr);
decimal start = Convert.ToDecimal(stime.Hour) + Convert.ToDecimal(stime.Minute) / 100;
decimal end = Convert.ToDecimal(etime.Hour) + Convert.ToDecimal(etime.Minute) / 100;
decimal nowtime = Convert.ToDecimal(now.Hour) + Convert.ToDecimal(now.Minute) / 100;
if (end < start)
{
if (nowtime >= start || nowtime <= end)
{
return true;
}
else
{
return false;
}
}
else
{
if (nowtime >= start && nowtime <= end)
{
return true;
}
else
{
return false;
}
}
}
#8
用TimeSpan+DateTime结合,很容易实现你的想法:
DateTime curr = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 8, 30, 0);//填实际值
DateTime t1 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 8, 0, 0);
DateTime t2 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 20, 0, 0);
TimeSpan span1 = t1.Subtract(curr);
TimeSpan span2 = t2.Subtract(curr);
if(span1.TotalSeconds<=0 && span2.TotalSeconds>=0)
{
//落在区间范围
}
DateTime curr = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 8, 30, 0);//填实际值
DateTime t1 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 8, 0, 0);
DateTime t2 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 20, 0, 0);
TimeSpan span1 = t1.Subtract(curr);
TimeSpan span2 = t2.Subtract(curr);
if(span1.TotalSeconds<=0 && span2.TotalSeconds>=0)
{
//落在区间范围
}
#9
DateTime dt1 = new DateTime(DateTime.Now.Year, DateTime.Now.Month,DateTime.Now.Day,9,0,0);
DateTime dt2 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 18, 0, 0);
TimeSpan ts1 = dt1.Subtract(DateTime.Now);
TimeSpan ts2 = dt2.Subtract(DateTime.Now);
if (ts1.TotalSeconds < 0 && ts2.TotalSeconds > 1)
{
Response.Write("OK");
}