Solution 1 :

change your input type like this.

<input type="datetime-local" name="Date" id="Date">

then use this js code to set current date.

var now = new Date();
now.setMinutes(now.getMinutes() - now.getTimezoneOffset());
document.getElementById('Date').value = now.toISOString().slice(0,16);

Solution 2 :

I think you should change your input tag’s type to datetime

<input type="datetime" id="Date" name="Date"/>

Problem :

I have here a script to auto-input the current date on my input tag. However, I cannot add an auto timestamp like date.getHour(); as well. It’s not working when I tried doing this.

Please take a look at the script I’m using:

        var date = new Date();

        var day = date.getDate();
        var month = date.getMonth() + 1;
        var year = date.getFullYear();

        if (month < 10) month = "0" + month;
        if (day < 10) day = "0" + day;

        var today = year + "-" + month + "-" + day;


        document.getElementById('Date').value = today;
    <form id="saveNote">
    
        <textarea class="lined" id="textarea1" name="textarea1" spellcheck="true" placeholder="" onpaste="console.log('onpastefromhtml')"></textarea>

        <input type="date" id="Date" name="Date"/>

    </form>

    <button onclick="save()">Submit</button>

All I’m looking for is a simple solution that will add a timestamp at the end of the day like “1/12/2021 – 02:00 PM” or even in 24-hour format.

EDIT: Here’s my failed code:

var date = new Date();

var day = date.getDate();
var month = date.getMonth() + 1;
var year = date.getFullYear();
var hour = date.getHours()

if (month < 10) month = "0" + month;
if (day < 10) day = "0" + day;

var today = year + "-" + month + "-" + day + "-" + hour;


document.getElementById('Date').value = today;

Thanks in advance!

Comments

Comment posted by stackoverflow.com/questions/221294/…

try this

Comment posted by kmoser

“However, I cannot add an auto timestamp like date.getHour(); as well. It’s not working when I tried doing this.”

Comment posted by ixcode

Sorry, I updated it just now.

Comment posted by ixcode

Thank you, this work as needed! Is it possible to make the time auto-refresh without refreshing the browser? Like a “timer”? I have a code like this but I don’t know how to combine it with here.

Comment posted by w3schools.com/jsref/met_win_setinterval.asp

you can use this js script on a setInterval. please refer –

Comment posted by developer.mozilla.org/en-US/docs/Web/HTML/Element/input/…

Hi Mate @kiritoXF: Would like to say that ..From Docs 🙂 *This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it. *

By