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

编程入门 行业动态 更新时间:2024-10-24 18:28:27
本文介绍了如何比较当前的时间对一天中的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I have an ASP 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页面名为:

Let's say I have two separate section in my myfile.inc file which is called from my main asp 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 } } }

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

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

更多推荐

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

本文发布于:2023-10-24 08:39:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1523465.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!