如何比较当前的时间对一天中的时间 [英] How to compare current time against time of day

查看:122
本文介绍了如何比较当前的时间对一天中的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有有不同的小时不同天操作的ASP.net页面。我所希望做的是比较反对今天的开放,关闭时间为当前时间。如果当前服务器时间处于的开放,关闭范围内显示的div内的open.png,否则显示的div内的close.png。

I have an ASP.net page which has different hours of operation for different day. What I am looking to do is compare the current time against today's open to close hours. If the current server time falls within the open to close range display a "open.png" inside a div otherwise display a "close.png" inside a div.

比方说,我在我的 myfile.inc 文件中的两个独立的部分,它是从我的主要asp.net页面名为:

Let's say I have two separate section in my myfile.inc file which is called from my main asp.net page:

<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#的code什么我现在:

The code of C# of what I have right now:

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

以上code只是显示当前时间。

The above code just displays the current time.

我怎样才能做到显示基于运行在C#中比较当前时间的小时为位置的开启和关闭的形象?

How can I accomplish displaying the open and close image for both location based on the hours of operation comparing to the current time in C#?

推荐答案

我修改了服务器端code创建,你可以从客户端使用的服务器标签调用两个字符串变量。只需将它们的网址为背景。 (我假定这就是你想要改变图像的位置)。

I modified the server side code to create two String variables that you can call from the client side using the server tags. Just place them in the url for the background. (I assume that's the location of the image you want to change).

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
            }

    }
}

//信用:这下面的静态函数是: http://stackoverflow.com/a/592258/2777098(@Daniel LeCheminant)

//Credit: this following static function is from: http://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;
    }

}

这篇关于如何比较当前的时间对一天中的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆