如何将当前时间与一天中的时间进行比较
本文关键字:时间 比较 一天 | 更新日期: 2023-09-27 17:55:21
我有一个 ASP.net 页面,不同日期的营业时间不同。我想做的是将当前时间与今天的开盘到收盘时间进行比较。如果当前服务器时间在打开到关闭范围内,则在div 内显示"打开.png",否则在div 内显示"关闭.png"。
假设我的myfile.inc
文件中有两个单独的部分,是从我的主 asp.net 页面调用的:
<div style="width: 100%; text-align: center; padding-left: 15px; padding-top: 25px;">
<div style="font-weight: bold; color: #00A0BE; position: relative; margin: 0 auto; width: 280px; height: 85px; background: url('theImages/labHoursHeader.png') no-repeat;">
<br />
210 Ave<br />
White Plains, New York 10964<br />
914.689.1542
</div>
<div style="text-align: center; position: relative; margin: 0 auto; width: 280px; height: 80px; background: url('theImages/labHoursHeaderMiddle.png') repeat-y;">
<div style="text-align: left; padding-left: 15px; width: 260px; margin: 0 auto;">
Monday & Thursday: 7AM - 7:30PM<br />
Tuesday & Wednesday: 7AM - 7PM<br />
Friday: 7AM - 5:30PM<br />
Saturday: 8AM - 1:30PM <br />
Sunday: Closed
</div>
</div>
<div style="position: relative; margin: 0 auto; width: 280px; height: 26px; background: url('theImages/labHoursHeaderFooter.png') no-repeat;">
</div>
</div>
<div style="width: 100%; text-align: center; padding-left: 15px; padding-top: 25px;">
<div style="font-weight: bold; color: #00A0BE; position: relative; margin: 0 auto; width: 280px; height: 85px; background: url('theImages/labHoursHeader.png') no-repeat;">
<br />
1 Road<br />
Rye, New York 10630<br />
914.325.8800 </div>
<div style="text-align: center; position: relative; margin: 0 auto; width: 280px; height: 80px; background: url('theImages/labHoursHeaderMiddle.png') repeat-y;">
<div style="text-align: left; padding-left: 15px; width: 260px; margin: 0 auto;">
Mon, Wed, & Fri: 8AM - 5:30PM<br />
Tuesday & Thursday: 8AM - 6PM<br />
Saturday: 8AM - 12PM <br />
Sunday: Closed
</div>
</div>
<div style="position: relative; margin: 0 auto; width: 280px; height: 26px; background: url('theImages/labHoursHeaderFooter.png') no-repeat;">
</div>
</div>
我现在拥有的 C# 代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class medical_specialties : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DateTime now = DateTime.Now;
string time = now.ToString("T");
Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + time + "');", true);
}
}
上面的代码只显示当前时间。
如何根据与 C# 中的当前时间相比的操作小时数显示两个位置的打开和关闭图像?
我修改了服务器端代码以创建两个 String 变量,您可以使用服务器标记从客户端调用它们。只需将它们放在背景的 url 中即可。(我假设这是您要更改的图像的位置)。
public partial class medical_specialties : System.Web.UI.Page
{
String url1 = "theImages/ClosedHeaderMiddle.png";
String url2 = "theImages/OpenHeaderMiddle.png";
String location1URL = ""; //White Plains
String location2URL = ""; //Rye
protected void Page_Load(object sender, EventArgs e)
{
DateTime now = DateTime.Now;
string time = now.ToString("T");
//Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + time + "');", true);
if(now.DayOfWeek == DayOfWeek.Monday){
if(IsTimeOfDayBetween(now, new TimeSpan(7, 0, 0), new TimeSpan(8, 0, 0) )) {
location1URL = url2;
location2URL = url1;
} else if(IsTimeOfDayBetween(now, new TimeSpan(8, 0, 0), new TimeSpan(17, 30, 0)) {
location1URL = url2;
location2URL = url2;
} else if(IsTimeOfDayBetween(now, new TimeSpan(17, 30, 0), new TimeSpan(19, 30, 0)) {
location1URL = url2;
location2URL = url1;
} else {
location1URL = url1;
location2URL = url1;
}
} else if(now.DayOfWeek == DayOfWeek.Tuesday) {
..... //just go on like the example above
}
}
}
信用:以下静态函数来自:https://stackoverflow.com/a/592258/2777098(@Daniel LeCheminant)
static public bool IsTimeOfDayBetween(DateTime time,
TimeSpan startTime, TimeSpan endTime)
{
if (endTime == startTime)
{
return true;
}
else if (endTime < startTime)
{
return time.TimeOfDay <= endTime ||
time.TimeOfDay >= startTime;
}
else
{
return time.TimeOfDay >= startTime &&
time.TimeOfDay <= endTime;
}
}