Get the current time and compare with an array
I would like to get the current time and compare it with an array
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;
}
}
NB: I know my problem is when I compare these two times, but how do I do it?
source to share
Here is the complete code
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>
JScript
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 Example
source to share
As I understand it, you want to compare the formatted string and filter the days array.
This can be done as follows:
var day = days.filter(
function(e) {
if(e.time === $scope.currentTime.getHours() + ":" + $scope.currentTime.getMinutes())
{ return e};
});
if(day.length > 0){
$scope.eee = day[0].channel;
}
I updated your fiddle: http://jsfiddle.net/ow48k6aa/ And it works for me.
But I think you really want to get the current HH: mm in Timespan from 08:00 to 08:30, right?
Than you could do this fiddle
var day = days.filter(
function(e) {
var t = e.time.split(":");
var minutes = parseInt(t[0])*60 + parseInt(t[1]);
var currMinutes = $scope.currentTime.getHours() * 60 + $scope.currentTime.getMinutes();
if(currMinutes < minutes)
{ return e};
});
if(day.length > 0){
$scope.eee = day[0].channel;
}
Attention! The above only works if the days array is sorted by time! Otherwise, it needs to be changed.
source to share
Another clean JS alternative for parsing your time. If you have a time stored as a 12 hour format, you can use en-US, or if it is a 24 hour format, you can use de-DE.
$scope.currentTime = new Date().toLocaleTimeString('en-US', {
hour: '2-digit',
minute: '2-digit'
}).replace(/\./g, '/');
With this, you can use your existing comparison. You can also expand it, depending on whether you want to have the entire date as well. Just use toLocaleDateString.
More on javascript here .
source to share