c# - 如何将当前时间与一天中的时间进行比较

标签 c# html asp.net css

我有一个 ASP.net 页面,不同日期的运行时间不同。我想要做的是将当前时间与今天的开放时间和关闭时间进行比较。如果当前服务器时间落在打开到关闭范围内,则在 div 内显示“open.png”,否则在 div 内显示“close.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# 中根据与当前时间相比的操作时间来显示两个位置的打开和关闭图像?

最佳答案

我修改了服务器端代码以创建两个字符串变量,您可以使用服务器标记从客户端调用它们。只需将它们放在背景 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;
    }

}

关于c# - 如何将当前时间与一天中的时间进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22769557/

相关文章:

css - 使用 css 将图像在图形内的链接内垂直居中

c# - 更改 MVC 主页登陆页面 : No service for type 'Microsoft.Extensions.DependencyInjection.IServiceCollection' has been registered

c# - 从 nupkg 读取元数据

c# - 无法在 JAVA 中验证签名,但在 .NET 中验证成功

html - 使用 bootstrap4 的可滚动表体

javascript - 滚动内部div

c# - 检查 asp.net 中的第二个参数

c# - Entity Framework 中的级联删除

asp.net - 单击列表时

c# - List inside 其他列表性能