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
  • 25,539
  • 11
  • 55
  • 67
Dranreb
  • 97
  • 2
  • 9
  • 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
  • 131,976
  • 10
  • 91
  • 107
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,437
  • 1
  • 26
  • 32