美文网首页
JS 获取当前日期为周几的几种写法

JS 获取当前日期为周几的几种写法

作者: _皓月__ | 来源:发表于2020-09-02 10:31 被阅读0次

一、

function getDayOfWeek(date) {
  var str = "";
  var week = new Date(date).getDay();
  if (week == 0) {
    str = "星期日";
  } else if (week == 1) {
    str = "星期一";
  } else if (week == 2) {
    str = "星期二";
  } else if (week == 3) {
    str = "星期三";
  } else if (week == 4) {
    str = "星期四";
  } else if (week == 5) {
    str = "星期五";
  } else if (week == 6) {
    str = "星期六";
  }
  return str;
}
getDayOfWeek('2020-9-2'); // 星期三

二、

function getDayOfWeek(date) {
  var arr = new Array("日", "一", "二", "三", "四", "五", "六");
  var week = new Date(date).getDay();
  var str = "星期" + arr[week];
  return str;
}
getDayOfWeek('2020-9-2'); // 星期三

三、

function getDayOfWeek(date) {
  var str = "星期" + "日一二三四五六".charAt(new Date(date).getDay());
  return str;
}
getDayOfWeek('2020-9-2'); // 星期三

相关文章

网友评论

      本文标题:JS 获取当前日期为周几的几种写法

      本文链接:https://www.haomeiwen.com/subject/fumisktx.html