Create a regressive counter (Countdown) from input and store in localStorage

Asked

Viewed 167 times

0

As you can see in my example below, I have a form that plays the data written in the entries (input) html and these are stored in localStorage.

In the input=date i need the date provided to be transformed into a countdown (countdown), for the remaining days.

<form id="form">
    <label>Name:</label>        <input type="text" id="name"><br>
    <label>Deadline:</label>    <input type="date" id="deadline"><br>
    <label>Image:</label>       <input type="file" id="pic">
    <input id="create" type="button" value="Create" />
</form>
<div id="tbody"></div>

<script>
$(function(){
$('#create').click(function () {
    var theName = $('#name').val();
    var theDeadline = $('#deadline').val();
    var pic = document.getElementById("pic").files[0];
    var imgUrl;
    var reader = new FileReader();  
    reader.onload = function(e) {
      var imgURL = reader.result;
      $('#tbody').prepend("<div>" + theName + "<br>" + theDeadline + "<br><img src=" + imgURL + "></div>");

      var tbody = $('#tbody').html();
      localStorage.setItem('tbody', tbody);
      saveDataToLocalStorage(imgURL);
    }
    reader.readAsDataURL(pic);
    return false;
});

$('#tbody').html(localStorage.getItem('tbody'));
return false;
});
</script>

example version in jsfiddle: https://jsfiddle.net/7qtk9gyh/

I really liked the puglin jquery.countdown.js, It’s very simple, however, as I’m new to scripts, I don’t even know how to make it work from an input and combined with the form. Any help is very welcome!

1 answer

0


You can turn your data inputs into milliseconds and calculate the difference between them

const date1 = new Date('8/1/2019'); // M/d/yyyy
const date2 = new Date(new Date().toDateString()); // cria uma data desconsiderando os milisegundos
const diffTime = Math.abs(date2.getTime() - date1.getTime());
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); 
console.log(`Data 1 = ${date1}`); 
console.log(`Data 2 = ${date2}`); 
console.log(`Diferença de tempo = ${diffTime}`); 
console.log(`Diferença de dias = ${diffDays}`);

So, as you want to store the result of the difference in the location, it would be necessary to just add to the end of the code

localStorage.setItem('countdown', diffDays)
  • Friend, I have difficulty in how to implement what happened to me, I am very new in this. You could pass me the example in fiddle?

  • I created that fiddle with the implementation in your code. I have removed all code that is not pertinent to date logic. I suggest you do this to simplify your future questions, leaving only exemplified what really needs help.

  • First of all, thank you very much. I thought it was important to make the example more complete so that they understand better how the context works. For example. I need the counter to be created in conjunction with the functioning of the form. In your solution I can’t "take" Countdown together with the other variables - there when I write elements by .prepend. and use the values dynamically (e.g.: "<a>" + varX +"</a>") -, because it is very necessary that it is possible to create more than one Countdown, one at each click on "create" as well as everything else, and not just replace it.

  • I think it would be necessary to have only one storage in the localStorage, which is the whole, "tbody" of the example. Unfortunately I don’t have enough knowledge to try to do this on my own :(

  • @Thiagosoubra, I created more that fiddle containing some implementations that may come to help you, including the full rescue of the Torage site. For anything else, I suggest you raise separate questions like "how to save HTML in localStorage", "how to count the difference between dates". I hope I’ve helped.

  • @Thiagosoubra, you made it?

  • Sorry for the delay, I ended up having to devote myself to studies. I still could not really test, but what you gave me is great and well explained

  • Thank you very much!!

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.