Golide
Golide

Reputation: 1009

Reset a counter at the start of every month

Im working on journal IDs in JavaScript format JV001/1/2018, JV002/1/2018 and so on (for January). I need to reset the counter at the start of every month so that journals IDs for February will be JV001/2/2018, JV002/2/2018 and so on.

The ID will be populated to an input element on page load. How can I use a single JavaScript function to do the counter, prototype.padStart and finally pass the value to an input element.

For my month and year I have:

<script type="text/javascript">
    function getDate(){
        var month = todaydate.getMonth() + 1;
        var year = todaydate.getFullYear();
        var datestring = month + "/" + year;
        document.getElementById("referenceNumber").value = datestring;
    }
    getDate();
</script>

which is rendering as below: enter image description here

Upvotes: 2

Views: 1535

Answers (1)

filipbarak
filipbarak

Reputation: 1905

I'd check if today's date - a counter is == 0.

So you will start the counter at January 1st and it would be one. Then you would get today's date and subtract it from the counter.

So, 1-1 = 0. Then as each day passes you would also increment the counter. Once getDate() passes 31, it would be 1, but the counter would be 32. So 1-32 != 0, and you reset the counter to match todays date.

Makes sense?

Upvotes: 1

Related Questions