Show Current Date and Time

Submitted by:David Villa

Date added:13 October, 2012

Category:JavaScript

Code example to show current date and time

Tags: current date , current time

Code Snippet:

<script type="text/javascript">

var displayDate1= 2;
var displayDate2 = 1;
var displayDateTime= 3;
var displayTime= 4;

function DisplayDateTime(dateStyle)
{
var today = new Date();
var dStr = "";

switch (dateStyle)
{
case displayDate1:
dStr = today.toDateString();
break;
case displayDateTime:
dStr = today.toLocaleString();
break;
case displayTime:
dStr = today.toLocaleTimeString();
break;
case displayDate2:
default:
dStr = today.toLocaleDateString();
break;
}
document.write(dStr);
}

</script>

<p><strong>displayDate1:</strong>
<script language=JavaScript>DisplayDateTime(displayDate2)</script>
</p>

<p><strong>displayDate2:</strong>
<script language=JavaScript>DisplayDateTime(displayDate1)</script>
</p>

<p><strong>displayDateTime:</strong>
<script language=JavaScript>DisplayDateTime(displayDateTime)</script>
</p>

<p><strong>displayTime:</strong>
<script language=JavaScript>DisplayDateTime(displayTime)</script>
</p>
 
 

Comments