forked from abhishek213-alb/CountdownTimer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request abhishek213-alb#11 from iamakhileshmishra/main
Custom Time Input From User Added For the Countdown
- Loading branch information
Showing
3 changed files
with
62 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,51 @@ | ||
var target_date = new Date("2022-03-25"); | ||
var days, hours, minutes, seconds; | ||
|
||
var countdown = document.getElementById("tiles"); | ||
var input_date = document.getElementById("date-c"); | ||
var target_date = new Date("0000-00-00"); | ||
var newCountdown = input_date.addEventListener("change", function () { | ||
var input = this.value; | ||
target_date = new Date(input); | ||
var curr = new Date().getTime(); | ||
if (target_date < curr) { | ||
alert("Date is from past and i am unble to to time travel."); | ||
console.log("This is Chutiyapa"); | ||
target_date = curr; | ||
} | ||
}); | ||
var days, hours, minutes, seconds; | ||
|
||
var countdown = document.getElementById("tiles"); | ||
|
||
getCountdown(); | ||
|
||
setInterval(function () { getCountdown(); }, 1000); | ||
setInterval(function () { | ||
getCountdown(); | ||
}, 1000); | ||
|
||
function getCountdown() { | ||
|
||
var current_date = new Date().getTime(); | ||
var seconds_left = (target_date - current_date) / 1000; | ||
|
||
days = pad(parseInt(seconds_left / 86400)); | ||
seconds_left = seconds_left % 86400; | ||
|
||
hours = pad(parseInt(seconds_left / 3600)); | ||
seconds_left = seconds_left % 3600; | ||
|
||
minutes = pad(parseInt(seconds_left / 60)); | ||
seconds = pad(parseInt(seconds_left % 60)); | ||
|
||
// format countdown string + set tag value | ||
countdown.innerHTML = "<span>" + days + "</span><span>" + hours + "</span><span>" + minutes + "</span><span>" + seconds + "</span>"; | ||
var current_date = new Date().getTime(); | ||
var seconds_left = (target_date - current_date) / 1000; | ||
|
||
days = pad(parseInt(seconds_left / 86400)); | ||
seconds_left = seconds_left % 86400; | ||
|
||
hours = pad(parseInt(seconds_left / 3600)); | ||
seconds_left = seconds_left % 3600; | ||
|
||
minutes = pad(parseInt(seconds_left / 60)); | ||
seconds = pad(parseInt(seconds_left % 60)); | ||
|
||
// format countdown string + set tag value | ||
countdown.innerHTML = | ||
"<span>" + | ||
days + | ||
"</span><span>" + | ||
hours + | ||
"</span><span>" + | ||
minutes + | ||
"</span><span>" + | ||
seconds + | ||
"</span>"; | ||
} | ||
|
||
function pad(n) { | ||
return (n < 10 ? '0' : '') + n; | ||
return (n < 10 ? "0" : "") + n; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters