簡體   English   中英

getMonth 返回不正確的值

[英]getMonth returns incorrect value

我有一些測試在我的代碼上測試 getMonth 方法。

為了一切順利,我將日期設置為 UTC 0,如果我輸入例如Date.UTC(new Date (2021, 0, 1))我有一個字符串2021-01-01T00:00:00.000Z

但是當我將它上傳到 gitlab 並通過那里的測試時,結果也是2021-01-01T00:00:00.000Z ,但是getMonth()給了我12值。

你能幫我嗎?

以下示例在我的測試1上返回,但在 gitLab 終端上返回12

console.log(
    "original time month__ ",
    new Date(
       new Date(year, month - 1, 1).getTime() -
          new Date().getTimezoneOffset() * 60 * 1000
    ).getMonth() + 1
);

對於 UTC,您必須使用getUTCMonth而不是getMonth

查看控制台屏幕截圖以獲取解釋。 (來自印度的測試 +05:30)

在此處輸入圖片說明

 let demo = new Date('2021-12-31T20:00:00.000Z'); //TZ: +05:30 console.log('getMonth(): ', demo.getMonth()); console.log('getUTCMonth(): ', demo.getUTCMonth());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM