我花了一個小時尋找答案并嘗試不同的事情,所以我很感激這里的任何幫助。
以下代碼非常適合查找某人的 B 部分生效日期。但是,當某人的生日真的是一個月的 1 日時,會使用“if”函式,我不再能夠格式化和寫入日期。這幾乎就像'partB_eff' 不再是一個日期物件。(我是新手,所以我可能只是在編造這個部分。)
我收到錯誤“TypeError:partB_eff.toLocaleDateString 不是 AutoFill_6_Step_Checklist 中的函式(代碼:24:27)”
我該如何解決這個問題?
let birthday = new Date(e.values[2]);
//this is a date entered from a google form
let bdayCopy = new Date(birthday);
//I created this since I'll be using .setMonth(), and I don't want to change the original date of the birhtday
let bday65 = new Date(bdayCopy.setMonth(bdayCopy.getMonth() 780));
//finds the 65th birthday
let partB_eff = new Date(bdayCopy.setDate(01));
//find's the Medicare part B effective date (the 1st of the month someone turns 65)
if(birthday.getDate()==1){
partB_eff = partB_eff.getMonth-1;
//if the person's birthday is really on the 1st of the month, the part b effective date is the 1st of the month prior. partB_eff must be converted
}
partB_eff = partB_eff.toLocaleDateString('en-us',{year:"numeric",month: "short",day:"numeric"});
//format partB_eff so that it looks nice on paper
uj5u.com熱心網友回復:
partB_eff = partB_eff.getMonth-1;
不做你認為它做的事。它所做的是從您的日期物件中獲取 vound函式getDate,并嘗試從中減去一個。在任何其他語言中,嘗試對函式進行減法都會是型別錯誤,但 Javascript 是 Javascript,并且幾乎允許對任何型別進行數字運算。JS 中的函式減去一個數就是NaN. NaN沒有名為 的方法toLocaleString,因此出現錯誤。
有趣的是,您在上面正確地執行了相同的操作bdayCopy.setMonth(bdayCopy.getMonth() 780)
Just do the same thing here
bdayCopy = new Date(bdayCopy.setMonth(bdayCopy.getMonth()-1));
還有一些重要的概念。if在 Javascript 中不是函式。if是開始條件陳述句的關鍵字。你不能做任何你可以用 if 函式做的事情。您不能呼叫它或將其分配給變數或將 ot 作為函式引數傳遞。清楚地了解什么是函式是您能夠在 JS 或坦率地說任何其他語言中作業所需要做的事情。
最后,如果您在 JS 中進行日期數學運算,我強烈建議您使用 DateFns 或 Moment 之類的日期庫。Javascript 原生日期 API 可能是任何語言中設計最差的日期 API。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/457676.html
標籤:javascript 日期 if 语句 格式
