IF函数是否在JavaScript中删除日期对象?
我花了一个小时寻找答案并尝试不同的事情,所以我很感谢这里的任何帮助。
以下代码非常适合查找某人的 B 部分生效日期。但是,当某人的生日确实是一个月的 1 号时,就会使用“if”函数,并且我无法再格式化和写入日期。这几乎就像“partB_eff”不再是日期对象。 (我是新手,所以我可能只是在编造这一部分。)
我收到错误“TypeError:partB_eff.toLocaleDateString 不是 AutoFill_6_Step_Checklist(Code: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
I've spent an hour looking for answers and trying different things so I appreciate any help here.
The following code works great for finding someone's part B effective date. However, when someone's birthday is really on the 1st of a month the 'if' function get's used, and I'm no longer able to format and write the date. It's almost like 'partB_eff' is no longer a date object. (I'm a newbie, so I might just be making this part up.)
I'm getting the error "TypeError: partB_eff.toLocaleDateString is not a function at AutoFill_6_Step_Checklist(Code:24:27)"
How can I resolve this?
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不做您认为做的事情。它的作用是从您的日期对象中获取Vound 函数 getDate,并尝试从中减去一个。在任何其他试图在函数上减法的语言中,都是类型错误,但是JavaScript是JavaScript,并且允许几乎任何类型的数字操作。函数减去JS中的数字是
nan
。NAN
没有一种称为tolocalestring
的方法,因此错误。有趣的是,您使用
bdaycopy.setmonth(bdaycopy.getMonth()+780)
正确地完成了相同的操作。在这里也要做同样的事情
,也要做一些重要的概念。
如果JavaScript中的
是不是一个函数。如果
是启动条件语句的关键字。您无法使用if函数来做任何事情。您无法称呼它或将其分配给变量或将OT作为函数参数。清楚地了解一个功能是您需要做的事情才能在JS或其他任何其他语言中工作。最后,如果您在JS中进行日期数学,我强烈建议您使用日期库,例如dateFNS或MONK。 JavaScript本地日期API可能是有史以来最糟糕的日期API。
Doesn't do what you think it does. What it does is get the vound function getDate from your date object, and attempt to subtract one from it. In any other language trying to do subtraction on a function would be a type error, but Javascript is Javascript and allows numeric operations on almost any type. A function minus a number in JS is
NaN
.NaN
doesn't have a method calledtoLocaleString
, hence the error.What's interesting is that you did the same operation correctly above with
bdayCopy.setMonth(bdayCopy.getMonth()+780)
Just do the same thing here
Also some important concepts.
if
in Javascript is not a function.if
is a keyword that starts a conditional statement. You can't do any of the things you can do with a function with if. You can't call it or assign it to a variable or pass ot as a function argument. Clearly understanding what a function is is something you need to do to be able to work in JS, or frankly any other language.Finally if you are doing date math in JS I strongly recommend you use a date library like DateFns or Moment. Javascript native date APIs are possibly the worst designed date API of any language ever.