moment.isoWeekday不是一个功能

问题描述 投票:0回答:1

我正在使用codesandbox,同样的错误一直在你身边:502: bad gateway。看着终端显然是因为moment.isoWeekday不是一个功能。为什么是这样?

我查看了moment.js,我把它放在我的代码中的方式显然是正确的。

var http = require("http");
var moment = require("moment");
moment().format();

function getDates() {
  var start = moment.utc("1st Jan 2019");
  var end = moment.utc("31st December 2019");
  var dates = [];
  var current = start.clone();

  if (current !== moment.isoWeekday(1)) {
    current = moment().add(1, "w");
  }
  while (current.isBefore(end)) {
    current.clone.push(dates);
    current = moment.add(2, "w");
  }

  return dates;
}

http
  .createServer(function(req, res) {
    res.writeHead(200, { "Content-Type": "text/plain" });
    res.write("day,date", "\n");
    var dates = getDates();
    for (var i = 0; i < dates.length; i++) {
      res.write(moment.format("dddd, Do MMMM YYYY", dates[i]), "\n");
    }
    res.end();
  })
  .listen(8080);

我正在做一个我需要输出日期的任务。 isoWeekday是代码的一部分,它应该检查当天是不是Monday,然后在变量上添加一周,以便在接下来的一周设置为Monday

javascript node.js momentjs httpserver
1个回答
1
投票

您的代码中有几个错误:

  1. 你在()之后忘记了moment.isoWeekday(1)
  2. moment.utc("1st Jan 2019")的输出为null,因为格式无法被识别,moment.utc("1st Jan 2019", "Do MMM YYYY")应该按预期工作
  3. 为了将current的克隆推入阵列dates,你必须做dates.push(current.clone());而不是current.clone.push(dates);
  4. moment.format("dddd, Do MMMM YYYY", dates[i])不正确,你必须改为dates[i].format("dddd, Do MMMM YYYY")

工作范例:

function getDates() {
  var start = moment.utc("1st Jan 2019", "Do MMM YYYY");
  var end = moment.utc("31st December 2019", "Do MMM YYYY");
  var dates = [];
  var current = start.clone();

  if (current.isoWeekday() !== 1) {
    //current = current.add(1, "w");
    const nextMonday = 1 + current.isoWeekday() + (7 - current.isoWeekday());
    current.day(nextMonday);
  }

  while (current.isBefore(end)) {
    dates.push(current.clone());
    current = current.add(2, "w");
  }

  return dates;
}

console.log("day, date");
var dates = getDates();
for (var i = 0; i < dates.length; i++) {
  console.log(dates[i].format("dddd, Do MMMM YYYY"));
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
© www.soinside.com 2019 - 2024. All rights reserved.