Category: Vanilla Javascript

Added: 25th of July 2021

Viewed: 1,232 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