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

2020-09-02  本文已影响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'); // 星期三
上一篇 下一篇

猜你喜欢

热点阅读