Printing the Current Date with JavaScript
This JavaScript will print the current date on a web page.
The script below will print the day of the week and the month name as part of the date.
It can easily be modified for languages other than English.
Example:
<script type="text/javascript"><!-- var now = new Date(); var Weekday = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"); var Month = new Array("January","February","March","April","May","June","July","August","September","October","November","December"); document.write(Weekday[now.getDay()]+", "+Month[now.getMonth()]+" "+now.getDate()+", "+now.getFullYear()); //--></script>
This JavaScript will print the date in m/d/y format. Change the output order and delimiting characters to suit your own date format preferences. Example:
<script type="text/javascript"><!-- var now = new Date(); document.write((now.getMonth()+1)+"/"+now.getDate()+"/"+now.getFullYear()); //--></script>
The output of this JavaScript is similar to the above except it will print the day and month as two digits, prepended with a 0 (zero) if necessary. It will print the date in mm/dd/yyyy format. Change the output order and delimiting characters to suit your own date format preferences. Example:
<script type="text/javascript"><!-- var now = new Date(); var month = now.getMonth()+1; if( month < 9 ) { month = "0"+month; } var day = now.getDate(); if( day < 9 ) { day = "0"+day; } document.write(month+"/"+day+"/"+now.getFullYear()); //--></script>
Will Bontrager