Current day with JavaScript

Finding the current day in JavaScript and taking it one step further. With this little snippet it is possible to make your users smile by greeting them with a happy ‘day’ message.


<script type="text/javascript">
  // Array of day names
  var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
  var now = new Date();

  document.write("Happy " + dayNames[now.getDay()] + ".");

</script>

Jake Bresnehan

A Front End Developer based in Sydney and the creator of Web Design Weekly.

Web Design Weekly keeps developers at the forefront of techniques and trends whilst helping them learn and grow to produce top quality code to make the web a better place for all.

1 Comment

  1. I think when use array in JavaScript, you can use:

    var dayNames = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];

    instead of:

    var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");

This post currently has one response. What do you think?

You can use basic HTML when posting code, please turn all < characters into &lt; or > into &gt;
If the code is multi-line, use <pre><code></code></pre>

Leave a Reply

Your email address will not be published. Required fields are marked *

Current day month ye@r *