0

I have this javascript that will determine the day of the week:

<script>
 function myFunction() {
   var d = new Date();
   var n = d.getDay()
   document.getElementById("demo").innerHTML = n;
 }
</script>

<p id="demo"></p>


Running this code, it'll display 4. Is there anyway I can convert this to an actual string/word like thursday?

user3942918
  • 24,679
  • 11
  • 53
  • 67
Dranreb
  • 97
  • 1
  • 8
  • http://stackoverflow.com/questions/9677757/how-to-get-the-day-of-the-week-from-the-day-number-in-javascript – barbsan Apr 06 '17 at 06:34

2 Answers2

2

you should use an array

 var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
 var day = days[ now.getDay() ];
ScaisEdge
  • 129,293
  • 10
  • 87
  • 97
1

You can use that number as an index to an array of actual day names:

var days = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'];
var date = new Date();
var day = days[date.getDay()];
Arnelle Balane
  • 5,272
  • 1
  • 26
  • 31