I am new to HTML and am facing difficulties in a task I am currently working on.
I am to display the current date in a format like this : "7 October 2022 Friday 10:47" in a non-editable textbox. The date, time etc. is supposed to change as the user refreshes the page.
For example, when a user refreshes the page after one minute it should display "7 October 2022 Friday 10:48" and if the user accesses the page on the next day at the same time it should display "8 October 2022 Saturday 10:47".
Here's what I did for the textbox:
<label for = "date-time"> Date & Time:</label>
<input type ="read only" name= "date-time" id = "date" readonly>
And this is the in line Javascript that I have done:
<script>
function getDate()
{
var todayDate = new Date();
document.getElementById("date")= todayDate;
}
</script>
Instead when I run this code, I get an empty non-editable textbox. I have tried to search up what is wrong but I don't seem to find my issue.
My <script>
is also placed at the end of <body>
, so I am unsure of what I am doing wrong.
EDIT: Updated code that displays it!
<label for = "date-time"> Date & Time:</label>
<input type ="text" id = "date" readonly>
<script>
function getDate()
{
var todayDate = new Date();
document.getElementById("date").value= todayDate;
}
</script>