1

I'm creating a countdown timer for a user selected time. For that I have developed following function.

function countdownTimeStart(){

var countDownDate = new Date("Sep 5, 2018 15:37:25").getTime();

var x = setInterval(function() {

    // Get to days date and time
    var now = new Date().getTime();

    // Find the distance between now an the count down date
    var distance = countDownDate - now;

    // Time calculations for days, hours, minutes and seconds
    var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = Math.floor((distance % (1000 * 60)) / 1000);

    // Output the result in an element with id="demo"
    document.getElementById("demo1").innerHTML = hours + ": "
        + minutes + ": " + seconds + " ";

    // If the count down is over, write some text
    if (distance < 0) {
        clearInterval(x);
        document.getElementById("demo1").innerHTML = "EXPIRED";
    }
}, 1000);
 }

This works fine. But I want to get a user selected value from a text input instead of var countDownDate = new Date("Sep 5, 2018 15:37:25").getTime();

Such as

<input type = "text" id = "picker-dates" value="08:30:20">

So can anyone help me to get this input value to my javascript function.

6
  • research .value Commented Apr 27, 2018 at 7:50
  • For what year/month/day? They are not defined in the element. Commented Apr 27, 2018 at 7:50
  • For hour,minutes and seconds Commented Apr 27, 2018 at 7:51
  • You may want to use type="date" or type="datetime-local" instead of type="text"developer.mozilla.org/en-US/docs/Web/HTML/Element/input/… Commented Apr 27, 2018 at 7:55
  • @ChathuriFernando was the answer helpful? Commented Apr 27, 2018 at 8:36

3 Answers 3

3

First get the time value from the input using the getElementById and then split that value with colon : to get the hour, minutes and seconds. With these value you can use setHours in the current date to set the time specified in the input.

function countdownTimeStart(){
var time = document.getElementById("picker-dates").value;
time = time.split(':');
var date = new Date();
var countDownDate = date.setHours(time[0],time[1],time[2]);
var x = setInterval(function() {
    // Get to days date and time
    var now = new Date().getTime();

    // Find the distance between now an the count down date
    var distance = countDownDate - now;

    // Time calculations for days, hours, minutes and seconds
    var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = Math.floor((distance % (1000 * 60)) / 1000);

    // Output the result in an element with id="demo"
    document.getElementById("demo1").innerHTML = hours + ": "
        + minutes + ": " + seconds + " ";

    // If the count down is over, write some text
    if (distance < 0) {
        clearInterval(x);
        document.getElementById("demo1").innerHTML = "EXPIRED";
    }
  }, 1000);
}
 
 countdownTimeStart();
<input type = "text" id = "picker-dates" value="14:30:20">
<div id='demo1'></div>

Sign up to request clarification or add additional context in comments.

6 Comments

The answer is very helpful, But there is a problem, the value 14:30:20. But the count start from another time. something like 6:42:20
Because @ChathuriFernando you are using the difference time for count down timer as in your code var distance = countDownDate - now; you can simply set the distance with the value of countDownDate as var distance = countDownDate to get started from the user input value.
Should I use var distance = countDownDate instead of var distance = countDownDate - now; ?
@ChathuriFernando yes, If you want the timer to start from the input value
But then not countdown the time
|
0

You can do like this, please have a look and if it doesn't work please let me know.

var selectedValue = document.getElementById("picker-dates").value;

3 Comments

@ChathuriFernando doing that much will not work. You need to add some more date operations to make it work
So can you please show the way for work my code since I'm new to the javascript
Here I'm not considering about the date or year, I just only need to count time for hour,minutes and seconds
-1

use jquery: $('#picker-dates').val()

1 Comment

not everyone wants to use jquery - it's added mb to your site, as well as having to learn a slightly different syntax

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.