如果日期是本周,本月或今年,则会获得 [英] momentjs get if date is this week, this month or this year

查看:52
本文介绍了如果日期是本周,本月或今年,则会获得的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将随机日期值传递给片刻,并检查日期是本周,本月还是今年,但似乎没有简单的功能可以在不写大量功能的情况下实现,但是因为我是初学者我不知道。

I am trying to pass random date values to moment and check if the date is this week, this month or this year but it seems there is no simple function to achive this without writing lots of function, but since I am a beginner I don't know.

现在唯一对我有用的就是这样的一周

The only thing that is working for me right now is getting the week like this

moment(somedate, "MM-DD-YYYY").week() // 44

我可以将它与今天的日期周比较

and I can compare that with todays date week

moment(todaysDate, "MM-DD-YYYY").week() // 44

并检查值是否相同以确定日期是否与此相关一周

and check if the values are the same to determine if the date is withing this week

但我的问题是我必须和月份和年份相同而且有点麻烦,我想通过寻找任何回答来解决问题。

but my problem is that I have to the the same to month and year and it's a bit cumbersome and I want to try to fix issue by looking for any answeres.

推荐答案

您可以使用查询(查看 docs

You can use queries (check docs)

moment(dateToCheck).isSame(new Date(), 'week'); //true if dates are in the same week
moment(dateToCheck).isSame(new Date(), 'month'); //true if dates are in the same month
moment(dateToCheck).isSame(new Date(), 'year'); //true if dates are in the same year

这篇关于如果日期是本周,本月或今年,则会获得的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆