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!

ixcode
  • 611
  • 8
  • 17
  • try this https://stackoverflow.com/questions/221294/how-do-you-get-a-timestamp-in-javascript – Programnik Jan 12 '21 at 06: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. – kmoser Jan 12 '21 at 06:30
  • Sorry, I updated it just now. – ixcode Jan 12 '21 at 06:34

2 Answers2

1

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

<input type="datetime" id="Date" name="Date"/>
kiritoXF
  • 181
  • 5
  • 1
    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. * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/datetime And still this may not work – Imran Rafiq Rather Jan 12 '21 at 06:47
  • It doesn't work again. – jovialcore Dec 29 '22 at 14:41
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);
Fir3
  • 148
  • 2
  • 10
  • 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. – ixcode Jan 12 '21 at 06:52
  • 1
    you can use this js script on a setInterval. please refer - https://www.w3schools.com/jsref/met_win_setinterval.asp – Fir3 Jan 12 '21 at 06:56