IF函数是否在JavaScript中删除日期对象?

发布于 2025-01-19 20:09:26 字数 1150 浏览 0 评论 0原文

我花了一个小时寻找答案并尝试不同的事情,所以我很感谢这里的任何帮助。

以下代码非常适合查找某人的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我三岁 2025-01-26 20:09:26
partB_eff = partB_eff.getMonth-1;

不做您认为做的事情。它的作用是从您的日期对象中获取Vound 函数 getDate,并尝试从中减去一个。在任何其他试图在函数上减法的语言中,都是类型错误,但是JavaScript是JavaScript,并且允许几乎任何类型的数字操作。函数减去JS中的数字是nanNAN没有一种称为tolocalestring的方法,因此错误。

有趣的是,您使用bdaycopy.setmonth(bdaycopy.getMonth()+780)正确地完成了相同的操作。
在这里也要做同样的事情

bdayCopy = new Date(bdayCopy.setMonth(bdayCopy.getMonth()-1));

,也要做一些重要的概念。 如果JavaScript中的不是一个函数。 如果是启动条件语句的关键字。您无法使用if函数来做任何事情。您无法称呼它或将其分配给变量或将OT作为函数参数。清楚地了解一个功能是您需要做的事情才能在JS或其他任何其他语言中工作。

最后,如果您在JS中进行日期数学,我强烈建议您使用日期库,例如dateFNS或MONK。 JavaScript本地日期API可能是有史以来最糟糕的日期API。

partB_eff = partB_eff.getMonth-1;

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 called toLocaleString, 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

bdayCopy = new Date(bdayCopy.setMonth(bdayCopy.getMonth()-1));

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文