0

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!

3
  • try this stackoverflow.com/questions/221294/… Commented Jan 12, 2021 at 6:27
  • "However, I cannot add an auto timestamp like date.getHour(); as well. It's not working when I tried doing this." Your code does not call date.getHour(). We can't help you fix your code unless you show us your code that is not working. Commented Jan 12, 2021 at 6:30
  • Sorry, I updated it just now. Commented Jan 12, 2021 at 6:34

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

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.
you can use this js script on a setInterval. please refer - w3schools.com/jsref/met_win_setinterval.asp
1

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

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

2 Comments

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. * developer.mozilla.org/en-US/docs/Web/HTML/Element/input/… And still this may not work
It doesn't work again.

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.