The code changes the new Date() to DayHourMinute
e.g. monday9AM45minutes to 010945
What I use is 010945 and my code specifies
if the var is between >=010921 && <=011010
change the background to green else nothing
But nothing happens and if I use alert(Time) it gives the message 010945.
How can if fix this?
Code:
function one() {
now = new Date();
hour = "" + now.getHours();
if (hour.length == 1) {
hour = "0" + hour;
}
minute = "" + now.getMinutes();
if (minute.length == 1) {
minute = "0" + minute;
}
day = "" + now.getDay();
if (day.length == 1) {
day = "0" + day;
}
var Time = day + '' + hour + '' + minute;
if (Time >= 010835 && Time <= 010920) {
document.getElementById('Man1').style.background = 'green';
} else {
document.getElementById('Man1').style.background = '';
}
if (Time >= 010921 && Time <= 011010) {
document.getElementById('Man2').style.background = 'green';
} else {
document.getElementById('Man2').style.background = '';
}
if (Time >= 011011 && Time <= 011105) {
document.getElementById('Man3').style.background = 'green';
} else {
document.getElementById('Man3').style.background = '';
}
if (Time >= 011106 && Time <= 011155) {
document.getElementById('Man4').style.background = 'green';
} else {
document.getElementById('Man4').style.background = '';
}
if (Time >= 011156 && Time <= 011239) {
document.getElementById('Man5').style.background = 'green';
} else {
document.getElementById('Man5').style.background = '';
}
if (Time >= 011240 && Time <= 011325) {
document.getElementById('Man6').style.background = 'green';
} else {
document.getElementById('Man6').style.background = '';
}
if (Time >= 011326 && Time <= 011415) {
document.getElementById('Man7').style.background = 'green';
} else {
document.getElementById('Man7').style.background = '';
}
if (Time >= 011416 && Time <= 011505) {
document.getElementById('Man8').style.background = 'green';
} else {
document.getElementById('Man8').style.background = '';
}
if (Time >= 011506 && Time <= 011555) {
document.getElementById('Man9').style.background = 'green';
} else {
document.getElementById('Man9').style.background = '';
}
}
setInterval(one, 1000);
Fiddle
Time is a string, and you are comparing to an int. Put the values in quotes:
if ( Time>='020000' && Time<='030000' ){
Related
I have a timer function, I want to change the value of my variable after the timer reaches 0?
function countdown(minutes) {
var seconds = 60;
var mins = minutes
function tick() {
var counter = document.getElementById("timer");
current_minutes = mins - 1
seconds--;
counter.innerHTML =
current_minutes.toString() + ":" + (seconds < 10 ? "0" : "") + String(seconds);
if (parseInt(current_minutes) === 0 && parseInt(seconds) === 00) {
sendAgain.show();
finalMin = parseInt(current_minutes);
finalSec = parseInt(seconds);
}
if (seconds > 0) {
timeoutHandle = setTimeout(tick, 1000);
} else {
if (mins > 1) {
// countdown(mins-1); never reach “00″ issue solved:Contributed by Victor Streithorst
setTimeout(function () { countdown(mins - 1); }, 1000);
}
}
}
tick();
}
variables :
let finalMin = null;
var finalSec = null;
I want the variables to be set after the following code is executed?
if (parseInt(current_minutes) === 0 && parseInt(seconds) === 00) {
sendAgain.show();
finalMin = parseInt(current_minutes);
finalSec = parseInt(seconds);
}
countdown(2);
console.log(finalMin); ===== show "0"
this is my code someone can help me i cant use if else on get current date i try all google tutorial no one work for me please good help for me if someone help me thank you in advance the only problem if else if i run this code if else wont read it
<script type="text/javascript">
function calc() {
var today = new Date();
var month = today.getMonth(); // Returns 9
console.log(month); // Output: 9
var textValue3 = document.getElementById('input3').value;
var textValue2 = document.getElementById('input2').value
var textValue1 = document.getElementById('input1').value;
var basic = 5;
if (month = '1') {
var rate_interest = 0;
}
else if (month = '2') {
var rate_interest = 0;
}
else if (month = '3') {
var rate_interest = 0.06;
}
else if (month = '4') {
var rate_interest = 0.08;
}
else if (month = '5') {
var rate_interest = 0.10;
}
else if (month = '6') {
var rate_interest = 0.12;
}
else if (month = '7') {
var rate_interest = 0.14;
}
else if (month = '8') {
var rate_interest = 0.16;
}
else if (month = '9') {
var rate_interest = 0.18;
}
else if (month = '10') {
var rate_interest = 0.20;
}
else if (month = '11') {
var rate_interest = 0.22;
}
else if (month = '12') {
var rate_interest = 0.24;
}
document.getElementById('output').value = (basic) + (textValue1 / 1000) + (textValue2 / 1000) + (textValue3 / 1000) + (basic * rate_interest);
}
</script>
In the 'if condition' you need to write == instead of =
date.getMonth() return the month in 0 to 11 so you need to plus one in a month.
function calc() {
var today = new Date();
var month = today.getMonth(); // Returns 9
month = month + 1;
console.log(month); // Output: 9
var textValue3 = document.getElementById('input3').value;
var textValue2 = document.getElementById('input2').value
var textValue1 = document.getElementById('input1').value;
var basic = 5;
var rate_interest;
if (month == 1) {
rate_interest = 0;
}
else if (month == 2) {
rate_interest = 0;
}
else if (month == 3) {
rate_interest = 0.06;
}
else if (month == 4) {
rate_interest = 0.08;
}
else if (month == 5) {
rate_interest = 0.10;
}
else if (month == 6) {
rate_interest = 0.12;
}
else if (month == 7) {
rate_interest = 0.14;
}
else if (month == 8) {
rate_interest = 0.16;
}
else if (month == 9) {
rate_interest = 0.18;
}
else if (month == 10) {
rate_interest = 0.20;
}
else if (month == 11) {
rate_interest = 0.22;
}
else if (month == 12) {
rate_interest = 0.24;
}
document.getElementById('output').value = (basic) + (textValue1 / 1000) + (textValue2 / 1000) + (textValue3 / 1000) + (basic * rate_interest);
}
I'm trying to create a chess clock using JavaScript. I've settle almost all of the issues with the clock's functionality except for this one. I'm using setInterval to essentially create the "countdown" effect on each player's clock. However, when it is triggered with the method I'm currently using the clock flickers. From my understanding/research, this is likely because there are overlapping 'intervals' that cause the code to try to display multiple intervals at the same time. Essentially the clock buttons (stopclock and stopclock2) when clicked call the functions on the bottom of my code. Could anyone provide some input on my code (below) and why this might be occurring?
clear1mils = "";
clear2mils = "";
//Clock Functions
function countdownmils2() {
document.getElementById("milsvalue2").innerHTML = --mil2;
if (mil2 == 0) {
mil2 = mil2 + 59;
if (sec2 == 0) {
if (min2 == 0) {
clearInterval(clear2mils);
document.getElementById("milsvalue2").innerHTML = "00";
}else if (min2 !== 0) {
sec2 = 60;
document.getElementById("minutesvalue2").innerHTML = --min2;
document.getElementById("secondsvalue2").innerHTML = --sec2;
}
}else if (sec2 !== 0) {
document.getElementById("secondsvalue2").innerHTML = --sec2;
if (sec2 <= 10) {
document.getElementById("secondsvalue2").innerHTML = "0" + sec2;
}
}
}else if (mil2 <= 10) {
document.getElementById("milsvalue2").innerHTML = "0" + mil2;
}
}
function countdownmils() {
document.getElementById("milsvalue").innerHTML = --mil;
if (mil == 0) {
mil = mil + 59;
if (sec == 0) {
if (min == 0) {
clearInterval(clear1mils);
document.getElementById("milsvalue").innerHTML = "00";
}else if (min !== 0) {
sec = 60;
document.getElementById("minutesvalue").innerHTML = --min;
document.getElementById("secondsvalue").innerHTML = --sec;
}
}else if (sec !== 0) {
document.getElementById("secondsvalue").innerHTML = --sec;
if (sec <= 10) {
document.getElementById("secondsvalue").innerHTML = "0" + sec;
}
}
}else if (mil <= 10) {
document.getElementById("milsvalue").innerHTML = "0" + mil;
}
}
//Clock 1
document.querySelector("#stopclock").addEventListener("click", () => {
clear2mils = setInterval(countdownmils2, 10);
clearInterval(clear1mils);
document.getElementById("stopclock").innerHTML = "DOWN";
document.getElementById("stopclock2").innerHTML = "UP";
document.getElementById("stopclock").setAttribute("disabled", "true");
document.getElementById("stopclock2").removeAttribute("disabled", "true");
});
//Clock 2
document.querySelector("#stopclock2").addEventListener("click", () => {
clear1mils = setInterval(countdownmils, 10);
clearInterval(clear2mils);
document.getElementById("stopclock2").innerHTML = "DOWN";
document.getElementById("stopclock").innerHTML = "UP";
document.getElementById("stopclock2").setAttribute("disabled", "true");
document.getElementById("stopclock").removeAttribute("disabled", "true");
});
I am trying to make the input date of birth mask in angular and format of date is dd/mm/yyyy ,but it not set and return the input according to our requirement input value.
my code given below.
<input type="text" placeholder="{{timePlaceholder}}" (focus)="showlable()" (focusout)="hidelable()" (keypress)="this.value =fixDatePattern($event);">
currentDate:any = "";
currentLength:any ="";
lastNumberEntered:any ="";
transformedDate:any="";
dateCountTracker:any="";
fixDatePattern(event) {
this.currentDate = event.target.value;
this.currentLength = this.currentDate.length;
this.lastNumberEntered = this.currentDate[this.currentLength - 1];
if (this.currentLength > 10) {
return this.currentDate.substring(0, 10);
}
if (this.currentLength == 1 && this.currentDate > 1) {
this.transformedDate = "0" + this.currentDate + '/';
this.dateCountTracker = 2;
this.currentLength = this.transformedDate.length;
return this.transformedDate;
} else if (this.currentLength == 4 && this.currentDate[3] > 3) {
this.transformedDate = this.currentDate.substring(0, 3) + "0" + this.currentDate[3] + '/';
this.dateCountTracker = 5;
this.currentLength = this.transformedDate.length;
return this.transformedDate;
} else if (this.currentLength == 2 && (this.dateCountTracker != 2 && this.dateCountTracker != 3)) {
this.dateCountTracker = this.currentLength;
return this.currentDate + '/';
} else if (this.currentLength == 5 && (this.dateCountTracker != 5 && this.dateCountTracker != 6)) {
this.dateCountTracker = this.currentLength;
return this.currentDate + '/';
}
this.dateCountTracker = this.currentLength;
return this.currentDate;
}
<input placeholder="mm/dd/yyyy" (input)="KeyUpCalled($event.target.value)" maxlength="10" [(ngModel)]="inputValue">
inputValue;
KeyUpCalled(value){
var dateCountTracker;
var currentDate = value;
var currentLength = currentDate.length;
var lastNumberEntered = currentDate[currentLength - 1];
if (currentLength > 10) {
var res = currentDate.substring(0, 10)
this.inputValue = res;
return this.inputValue
}
if (currentLength == 1 && currentDate > 1) {
var transformedDate = "0" + currentDate + '/';
dateCountTracker = 2;
currentLength = transformedDate.length;
this.inputValue = transformedDate;
return this.inputValue;
} else if (currentLength == 4 && currentDate[3] > 3) {
var transformedDate = currentDate.substring(0, 3) + "0" + currentDate[3] + '/';
dateCountTracker = 5;
currentLength = transformedDate.length;
this.inputValue = transformedDate;
return this.inputValue;
} else if (currentLength == 2 && (dateCountTracker != 2 && dateCountTracker != 3)) {
dateCountTracker = currentLength;
this.inputValue = currentDate + '/'
return this.inputValue;
} else if (currentLength == 5 && (dateCountTracker != 5 && dateCountTracker != 6)) {
dateCountTracker = currentLength;
// return currentDate + '/';
this.inputValue = currentDate + '/'
return this.inputValue;
}
dateCountTracker = currentLength;
this.inputValue = currentDate;
}
Using primeng :
in app module :
import {InputMaskModule} from 'primeng/inputmask';
#NgModule({
imports: [
...
InputMaskModule,
FormsModule
],
for the HTML :
<div class="p-col-12 p-md-6 p-lg-4">
<span>Date</span>
<p-inputMask mask="99/99/9999" [(ngModel)]="val3" placeholder="99/99/9999" slotChar="mm/dd/yyyy"></p-inputMask>
</div>
source : https://www.primefaces.org/primeng/inputmask
For some reason, my timer does not stop when the time reaches 0. JSFiddle. Here is the constructor for the countdown timer I made:
var Timer = function(opts) {
var self = this;
self.opts = opts || {};
self.element = opts.element || null;
self.minutes = opts.minutes || 0;
self.seconds = opts.seconds || 30;
self.start = function() {
self.interval = setInterval(countDown, 1000);
};
self.stop = function() {
clearInterval(self.interval);
};
function countDown() {
if (self.minutes == 0 && self.seconds == 0) {
self.stop();
}
self.seconds--;
if (self.seconds <= 0) {
self.seconds = 59;
self.minutes--;
}
if (self.seconds <= 9) { self.seconds = '0' + self.seconds; }
self.element.textContent = self.minutes + ' : ' + self.seconds;
}
};
and here is my usage:
HTML:
<div id="timer"></div>
JavaScript:
var myTimer = new Timer({
minutes: 0,
seconds: 10,
element: document.querySelector('#timer')
});
myTimer.start();
Change your countdown function to
function countDown() {
self.seconds--; //Changed Line
if (self.minutes == 0 && self.seconds == 0) {
self.stop();
}
if (self.seconds < 0) { //Changed Condition. Not include 0
self.seconds = 59;
self.minutes--;
}
if (self.seconds <= 9) { self.seconds = '0' + self.seconds; }
self.element.textContent = self.minutes + ' : ' + self.seconds;
}
http://jsfiddle.net/8d5LLeoa/3/
You just need to rearrange your countdown function a bit. It will never be zero when it hits your code that checks for a zero value. The code below works.
function countDown() {
self.seconds--;
self.element.textContent = self.minutes + ' : ' + self.seconds;
if (self.minutes === 0 && self.seconds === 0) {
self.stop();
return;
}
if (self.seconds <= 0) {
self.seconds = 59;
self.minutes--;
}
if (self.seconds <= 9) { self.seconds = '0' + self.seconds; }
}
};