Software Developer

Display The Current Date on Your Website With JavaScript

If you want to display the current date on your website, use this script. Since it’s JavaScript, a browser-side language, the date will always be current according to the viewer’s time, anywhere in the world. This code takes the current date from the user’s computer, not from your server. (This is one of the times in which it is more beneficial to use JavaScript rather than PHP.) Copy and paste the following code into your website’s HTML wherever you want the date to appear. You can style the output however you like with CSS.

Use this code if you want to include the day of the week:

This will output the day and the date, like so:

Friday, December 21, 2012

<script>
<!-- Begin
var now = new Date();
var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();
function fourdigits(number)	{
	return (number < 1000) ? number + 1900 : number;
}
today =  days[now.getDay()] + ", " +
         months[now.getMonth()] + " " +
         date + ", " +
         (fourdigits(now.getYear())) ;
document.write(today);
//  End -->
</script>

Use this code if you want don’t want the day of the week:

This will output the date, like so:

December 21, 2012

<script>
<!-- Begin
var now = new Date();
var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();
function fourdigits(number)	{
	return (number < 1000) ? number + 1900 : number;
}
today =  months[now.getMonth()] + " " +
         date + ", " +
         (fourdigits(now.getYear())) ;
document.write(today);
//  End -->
</script>

By

We've 2 Responses

  1. September 24th, 2018 at 6:11 pm

    This is really nice. It works well!

    How would I modify this script so that the output was: day Month Year? For example, I need my date to look like this:

    24 September 2018

    avatar

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]