Category: Vanilla Javascript

Added: 25th of July 2021

Viewed: 158 times

Display date and time using Javascript

The following code displays the date and time in Javascript

<script type="text/javascript">

// Display date and time
var date = new Date();


Sun Jul 25 2021 03:20:44 GMT+0100 (British Summer Time)

If you wanted to grab the hours, minutes, seconds and year individually you could assign these to their own variables by using the getHours(), getMinutes(), getSeconds() and getFullYear() methods

<script type="text/javascript">

// Display hours, minutes, seconds, year
var date = new Date();

var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
var year = date.getFullYear();

document.write("Hours: " +hours+ "<br>");
document.write("Minutes: " +minutes+ "<br>");
document.write("Seconds: " +seconds+ "<br>");
document.write("Year: " +year);

Hours: 3
Minutes: 59
Seconds: 36
Year: 2021

Other Tips & Tutorials

Vanilla Javascript

Display date and time using Javascript

Posted: 25th of July 2021

Views: 158

Vanilla Javascript

Clear all form fields using Javascript

Posted: 25th of October 2020

Views: 931

Vanilla Javascript

Generate a random number between 1 and 5 using Math.floor and Math.random commands in Javascript

Posted: 22nd of October 2018

Views: 1770

Vanilla Javascript

Using document.getElementByID() in javascript to send form data

Posted: 15th of October 2018

Views: 1592

Vanilla Javascript

Using document write() in javascript.

Posted: 14th of October 2018

Views: 1937