检查输入日期是否是季度的开始
下午好!您能告诉我如何实施一个函数,该功能是否已通过的日期是本季度的开始? 我尝试使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不建议使用MomentJ。甚至作者建议使用替代方案进行新的发展。
在这种情况下,使用本机JavaScript很容易做到,因为毕竟没有那么多季度:
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:
我只会使用内置
date
类型。节点中宣传的哪些工作:
I would just use the built-in
Date
type.Which work as advertised in Node: