如何比较当前时间与一天中的时间

时间:2014-03-31 18:39:20

标签: 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#中当前时间相比的操作时间完成显示两个位置的打开和关闭图像?

1 个答案:

答案 0 :(得分:2)

我修改了服务器端代码以创建两个String变量,您可以使用服务器标签从客户端调用这些变量。只需将它们放在背景的网址中即可。 (我认为这是你想要改变的图像的位置)。

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;
    }

}