检查输入日期是否是季度的开始

发布于 2025-01-26 21:11:39 字数 611 浏览 4 评论 0原文

下午好!您能告诉我如何实施一个函数,该功能是否已通过的日期是本季度的开始? 我尝试使用MONCH.JS库实现验证,并

const isStartOfQuarter = (date: string) => {
  return moment(date).isSame(moment().quarter(moment(date).quarter()).startOf('quarter'), 'day')
}

在调用此功能的日期“ 2022-04-01”时创建一个函数,它返回true并正确工作,但是如果您尝试使用日期调用它,则可以“ 2023-04” -01“它无法正常工作并返回false,

我还需要制作一个函数,以检查通过的日期是季度的结束 为此创建了一个函数

const isEndOfQuarter = (date: string) => {
  return moment(date).isSame(moment().quarter(moment(date).quarter()).endOf('quarter'), 'day')
}

我在调用此功能的日期“ 2022-06-30”时 返回错误

您可以告诉我如何正确实施此功能吗?

Good afternoon! Can you please tell me how to implement a function that checks if the passed date is the beginning of the quarter?
I tried to implement validation using the moment.js library and created a function

const isStartOfQuarter = (date: string) => {
  return moment(date).isSame(moment().quarter(moment(date).quarter()).startOf('quarter'), 'day')
}

When calling this function with the date "2022-04-01" it returns true and works correctly, but if you try to call it with the date "2023-04-01" it does not work correctly and returns false

I also need to make a function that checks if the passed date is the end of the quarter
I created a function for this

const isEndOfQuarter = (date: string) => {
  return moment(date).isSame(moment().quarter(moment(date).quarter()).endOf('quarter'), 'day')
}

When calling this function with the date "2022-06-30" it returns true and works correctly, but if you try to call it with the date "2023-06-30" it does not work correctly and returns false

Can you please tell me how to implement this functionality correctly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

很糊涂小朋友 2025-02-02 21:11:39

我不建议使用MomentJ。甚至作者建议使用替代方案进行新的发展。

在这种情况下,使用本机JavaScript很容易做到,因为毕竟没有那么多季度:

const isStartOfQuarter = (date) =>
    ["01-01", "04-01", "07-01", "10-01"].includes(date.slice(5, 10)); 

const isEndOfQuarter = (date) =>
    ["03-31", "06-30", "09-30", "12-31"].includes(date.slice(5, 10)); 

console.log(isStartOfQuarter("2023-04-01"));

I would not suggest using momentjs. Even the authors recommend using alternatives for new developments.

In this case it is quite easy to do with native JavaScript, as there aren't that many quarters after all:

const isStartOfQuarter = (date) =>
    ["01-01", "04-01", "07-01", "10-01"].includes(date.slice(5, 10)); 

const isEndOfQuarter = (date) =>
    ["03-31", "06-30", "09-30", "12-31"].includes(date.slice(5, 10)); 

console.log(isStartOfQuarter("2023-04-01"));

巴黎夜雨 2025-02-02 21:11:39

我只会使用内置date类型。

const isStartOfQuarter = (dateStr) => { 
  const d = new Date(dateStr); 
  return d.getUTCDate()==1 && d.getUTCMonth()%3==0
}

const isEndOfQuarter = (dateStr) => { 
  let d = new Date(dateStr); 
  d.setDate(d.getDate()+1); 
  return isStartOfQuarter(d);
}

节点中宣传的哪些工作:

> isStartOfQuarter("2023-04-01")
true
> isEndOfQuarter("2023-06-30")
true

I would just use the built-in Date type.

const isStartOfQuarter = (dateStr) => { 
  const d = new Date(dateStr); 
  return d.getUTCDate()==1 && d.getUTCMonth()%3==0
}

const isEndOfQuarter = (dateStr) => { 
  let d = new Date(dateStr); 
  d.setDate(d.getDate()+1); 
  return isStartOfQuarter(d);
}

Which work as advertised in Node:

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