How to take a date with Javascript and insert an input Hidden form?

Asked

Viewed 608 times

3

I need javascript to fill automatically in a <input type="hidden" /> with the current date, and another 15 days after the current date.

It will be used for the date of issue of one ticket and the other for the due date of the ticket, and need the format aaaammdd.

<input name="dataEmissao" type="hidden" />
<script>
   ????????????????
</script>

and

<input name="dataVencimentoTit" type="hidden" />
<script>
    ????????????????
</script>

Note: can’t be PHP, because I’m using this code inside a code Theme block, in Wordpress. Inside does not work PHP code, only Javascript.

  • What format do you want to give the date? yyyymmdd?

  • 1

    The winning should be done on the side that generates billet, and not form. In fact, if it is hosted wordpress, could yes do with PHP, and usually is not the theme the best place for it.

  • 1

    Sicoob sent me an example that the due field is sent via post. I also do not agree, but require the completion of this input. :/

  • @Rafaelt check here:https://jsfiddle.net/74Ly8gbx/ is what you’re looking for?

  • So... is that I need to put this code in a specific location within the theme... I don’t know why is not interpreting php in it...

  • the formed must be yyyymmdd

  • @Rafaelt worse than even google does such things in the name of "standard". Even so, if you could generate for PHP it would be better. However, in JS it is not complicated. Let’s wait for answer.

  • It worked... only the format that came like this "Mon Jun 13 2016 12:52:12 GMT-0300 (Official Time of Brazil)"

  • @Sergio, only the yyymmdd format is missing. Thank you so much for now!!!

  • @Rafaelt joined an answer with this. Load the page again...

  • @Sergio Something went wrong... the result for the current date was "2016413".. I also need the month to have two digits.. the right one would be "20160613"

  • @Rafaelt you’re right, https://jsfiddle.net/3zymmpxb/1/

  • 1

    @Sergio Perfeito! Thank you very much!!!!

  • @Sergio Amigo, abusing... I need to reduce the time to 10 days. You sent 1296e6 for 15 days... I do not know how to do this account... can you pass the new value to 10 days please? Thanks again!

  • @Rafaelt just count from milliseconds. 10(dias) x 24(h) x 60(m) x 60(s) x 1000, that is to say: 864000000 which may be shortened 864e6.

  • @Sergio thank you so much again. Hug

Show 11 more comments

1 answer

1


How to set a date in one input and another fifteen days later in another?

The steps you need to follow are:

  • create a date object with the start day
  • create a new date object with more 1296e6 ms that other (because Javascript works in milliseconds)
  • have a function to format the output string, in this case YYYYMMDD
  • go to DOM to "search" the elements and set your .value

In practice:

function pad(s) {
    return (s < 10) ? '0' + s : s;
}

function formatarData(d) {
    return [d.getFullYear(), d.getMonth() - 1, d.getDate()].map(pad).join('');
}

var emissao = document.querySelector('input[name="dataEmissao"]');
var vencimento = document.querySelector('input[name="dataVencimentoTit"]');

var hoje = new Date();
var quinzeDias = new Date(hoje.getTime() + 1296e6);
emissao.value = formatarData(hoje);
vencimento.value = formatarData(quinzeDias);

Example: https://jsfiddle.net/3zymmpxb/1

Browser other questions tagged

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