javascript - Moment js 不适用于 1 月(第 0 个月)
问题描述
我知道现在月份的索引为 0。基本上我将 this.month 设置为 0 到 11 之间的任何整数并运行以下代码。出于某种原因,它不想更新一月的时刻变量(编号为 0)。出了什么问题,我该如何解决?
const d: Moment = moment();
if (this.month) d.month(this.month);
console.log("Month value: " + this.month);
console.log("Moment value: " + d.month());
Console.log 为 this.month = 2(三月)返回以下内容
月值:2 矩值:2
Console.log 为 this.month = 1(二月)返回以下内容
月值:1 时刻值:1
Console.log 为 this.month = 0(一月)返回以下内容
月值:0 矩值:1
解决方案
if (this.month)
this.month
如果是假值(如 0),则以下指令将不会运行。
正如 HMR 建议的那样,您可能希望使用===
运算符(或其否定!==
)来检查this.month
is not null
nor undefined
。
推荐阅读
- python - 有没有办法将 HEX 或 RGB 格式转换为文本?
- python - 在 Holoviews Overlay 中默认/以编程方式静音/停用某些数据系列
- sql - 将 DEFAULT 值设置为 MariaDB 中的行位置
- c# - 使用 .NET SDK 从 Azure 文件共享下载空文件时出现 InvalidRange 错误
- python - 如何将 Python 列表作为单个电子邮件发送
- grails - 适用于 Grails 4 的 Java 和 Tomcat 的最新/最佳版本?
- r - 将多组 jpg 图像组合成一个 jpg/png r
- dialogflow-cx - “请切换到 'us-east1-dialogflow.googleapis.com' 以访问位于 'us-east1' 中的资源” Dialogflow CX GO API V3
- gcc - 使用 gcc -c 选项时如何指定输出文件目录?
- html - 角到角对齐