如何让.getDay返回一个不是数字的东西?

时间:2017-08-29 10:32:45

标签: javascript

结果显示" Monday1" ,因为 .getDay 方法将1显示为星期一。

var dob = new Date("April 2,1984");
var weekDay = dob.getDay();
if (weekDay==1) {
document.write('Monday');
}
document.write(weekDay);

有没有办法摆脱这个数字,只留下星期一的字?

3 个答案:

答案 0 :(得分:1)

示例

返回工作日的名称(不只是一个数字):

var d = new Date();
var weekday = new Array(7);
weekday[0] =  "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";

var n = weekday[d.getDay()];

n的结果将是:

Tuesday

来源:https://www.w3schools.com/jsref/jsref_getday.asp

答案 1 :(得分:0)

嗯,我想你面临的问题是你必须写多个if else和check。您可以使用以下功能一劳永逸地完成

function myFunction() {
  var d = new Date();
  var weekday = new Array(7);
  weekday[0] = "Sunday";
  weekday[1] = "Monday";
  weekday[2] = "Tuesday";
  weekday[3] = "Wednesday";
  weekday[4] = "Thursday";
  weekday[5] = "Friday";
  weekday[6] = "Saturday";

  var n = weekday[d.getDay()];
  alert(n);
}

希望这有帮助。

答案 2 :(得分:0)

您可以尝试使用工作日数组

<script src="https://d3js.org/d3.v4.min.js"></script>
<svg width="500" height="400" viewbox="0 0 500 400">
  <text> </text>
</svg>