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>

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 ye@r *