C# 基于当前日期n时间的Div背景色
我可以根据当前日期和时间为四个div指定背景色吗?比如,如果是2014年2月1日下午1:15,我想更改以下四个div的背景色C# 基于当前日期n时间的Div背景色,c#,javascript,html,css,C#,Javascript,Html,Css,我可以根据当前日期和时间为四个div指定背景色吗?比如,如果是2014年2月1日下午1:15,我想更改以下四个div的背景色 <div class="timeRow"> <div class="timeRowCell> <a title="1/02/2014 12:00:00 PM"> </a> </div> <div class="timeRowCell>
<div class="timeRow">
<div class="timeRowCell>
<a title="1/02/2014 12:00:00 PM"> </a> </div>
<div class="timeRowCell>
<a title="1/02/2014 12:15:00 PM"> </a></div>
<div class="timeRowCell>
<a title="1/02/2014 12:30:00 PM"> </a></div>
<div class="timeRowCell>
<a title="1/02/2014 12:45:00 PM"> </a></div>
<div class="timeRowCell>
<a title="1/02/2014 01:00:00 PM"> </a></div>
.
.
.
.
.
<div class="timeRowCell>
<a title="1/02/2014 07:00:00 PM"> </a></div>
<div class="timeRowCell>
<a title="1/02/2014 07:15:00 PM"> </a></div>
<div class="timeRowCell>
<a title="1/02/2014 07:30:00 PM"> </a></div>
<div class="timeRowCell>
<a title="1/02/2014 07:45:00 PM"> </a></div>
<div class="timeRowCell>
<a title="1/02/2014 08::00 PM"> </a></div>
</div>
我认为这应该适合你
<div class="timeRowCell>
<a title="1/02/2014 01:00:00 PM"> </a> </div>
<div class="timeRowCell>
<a title="1/02/2014 01:15:00 PM"> </a></div>
<div class="timeRowCell>
<a title="1/02/2014 01:30:00 PM"> </a></div>
<div class="timeRowCell>
<a title="1/02/2014 01:45:00 PM"> </a></div>
var anchors=document.getElementsByTagName('a');//得到所有的锚
对于(var i=0;i 如果(DateInachor>=新日期(“2014年2月1日01:00:00 PM”)&&DateInachor您可以执行以下操作。它只能在时间上匹配,如果您愿意,也可以在时间和日期上匹配:
var anchors = document.getElementsByTagName('a'); // get All anchors
for (var i = 0; i < anchors.length; i++) {
var dateInAnchor = new Date(anchors[i].title); //anchor's title as date
if(dateInAnchor >= new Date("1/02/2014 01:00:00 PM") && dateInAnchor <= new Date("1/02/2014 01:45:00 PM")){ //when condition matches
var parentDiv = anchors[i].parentElement;
parentDiv.style.background = "yellow"; //change background of parent div
}
};
函数doBackground(){
var elements=document.querySelectorAll('.timeRowCell a');
var now=新日期();
var currentHour=now.getHours();
var ampm=当前小时<12?'am':'pm';
//要仅在时间上匹配,请使用此行
var re=new RegExp((当前小时%12 | | 12)+':\\d\\d:\\d\\d'+ampm,'i');
//要同时匹配日期,请使用以下行
函数z(n){返回(N您想将其更改为什么?只想将包含ancor标签的DIV的背景色从2014年1月2日01:00:00 PM更改为2014年1月2日01:45:00 PM的黄色。我希望我说得通。感谢您的快速回复。那么您的意思是,如果当前时间和标题时间相同,您想更改背景色吗?这将是m如果您使用24小时时间,这会更容易。您能将标题输出日期格式更改为unixtimestamp吗?我能知道24小时格式是如何实现的吗?在javascript中是否可以首先将锚定标题时间转换为24小时格式?请不要使用日期构造函数解析日期。这不可靠,并且会在不同的时间产生不同的结果t browers(是2014年2月1日还是1月2日?)。手动解析字符串(两行代码)。此外,这不需要在每个iTreation上创建两个日期对象。
function doBackground() {
var elements = document.querySelectorAll('.timeRowCell a');
var now = new Date();
var currentHour = now.getHours();
var ampm = currentHour < 12? 'am' : 'pm';
// To match on time only, use this line
var re = new RegExp((currentHour%12 || 12) + ':\\d\\d:\\d\\d ' + ampm, 'i');
// To match on date also, use these lines
function z(n){return (n<10?'0':'') + n}
var re = new RegExp(now.getDate() + '\\/' + z(now.getMonth()+1) +
'\\/' + now.getFullYear() +
' ' + (currentHour%12 || 12) +
':\\d\\d:\\d\\d ' + ampm, 'i'
);
// Loop over elements looking for matches to change the background of
for (var i=0, iLen=elements.length; i<iLen; i++) {
if (re.test(elements[i].title)) {
elements[i].parentNode.style.backgroundColor = 'red';
}
}
}