gpt4 book ai didi

javascript - 获取当前时间并与数组进行比较

转载 作者:可可西里 更新时间:2023-11-01 13:09:09 26 4
gpt4 key购买 nike

我想获取当前时间并将其与数组进行比较

HTML:

<div ng:app ng:controller="Scoper">
<p> Now Playing:</p>
{{nowPlaying}}
<br />
<p> Current Time:</p>
{{currentTime | date:'HH:mm'}} </div>

Angular.js:

function Scoper($scope) {

var days = [
{ id: 0 , time:'08:00', channel: 'Gaming'},
{ id: 0 , time:'08:30', channel: 'MUSIC'},
{ id: 0 , time:'09:00', channel: 'Bob the Builder'},
{ id: 0 , time:'09:30', channel: 'Power puff girls'},
{ id: 0 , time:'10:00', channel: 'Spongbob'},
];
/*Get Current time */

$scope.currentTime = new Date();

if(days.time === currentTime){
$scope.nowPlaying= days.channel;
}
}

注意:我知道我的问题出在我比较这两个时间时,但我该怎么做呢?

Sample on Fiddle

最佳答案

完整代码如下

HTML

<div ng-app>
<h2>Todo</h2>
<div ng-controller="Scoper">
<p> Now Playing:</p>
{{nowPlaying}}
<br />
<p> Current Time:</p>
{{currentTime | date:'HH:mm'}}

</div>
</div>

脚本

function Scoper($scope) {
var days = [
{ id: 0 , time:'20:20', channel: 'Gaming'},
{ id: 0 , time:'20:18', channel: 'MUSIC'},
{ id: 0 , time:'20:22', channel: 'Bob the Builder'},
{ id: 0 , time:'20:16', channel: 'Power puff girls'},
{ id: 0 , time:'20:18', channel: 'Spongbob'},
];
/*Get Current time */
var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = (currentTime.getMinutes() < 10? '0' : '') + currentTime.getMinutes();
var nowTime = hours + ":" + minutes;

$scope.currentTime = nowTime;
for (var i = 0; i < days.length; i++) {
if(days[i].time == nowTime){
$scope.nowPlaying= days[i].channel;
break;
}
else{
$scope.nowPlaying= "Nothing";
}
}
}

JSFiddle Sample

关于javascript - 获取当前时间并与数组进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27924004/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com