Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

momentjs get if date is this week, this month or this year

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.

like image 777
hidar Avatar asked Nov 03 '17 13:11

hidar


People also ask

How do I get current year in MomentJs?

js moment(). year() Method. The moment(). year() method is used to get or set the year of the Moment object.

How do you find the week of the month in a moment?

let todaysDate = moment(moment. now()); let endOfLastMonth = moment(get(this, 'todaysDate')). startOf('month'). subtract(1, 'week'); let weekOfMonth = todaysDate.

How do I compare two dates in MomentJs?

To compare two dates in moment js, The momentjs provide isBefore() , isSame() , isAfter() , isSameOrBefore() , isSameOrAfter() , and isBetween() to compare dates as you want.

Is MomentJs being deprecated?

MomentJs recently announced that the library is now deprecated. This is a big deal for the javascript community who actively downloads moment almost 15 million times a week. With that I began a journey during a Hackathon to replace moment in a core library at my company.


3 Answers

Try to use moment isSame() method to check the date is with the limit.

First you have to add the moment dependencies in your project.

npm install moment --save   # npm
yarn add moment             # Yarn
Install-Package Moment.js   # NuGet
spm install moment --save   # spm
meteor add momentjs:moment  # meteor
bower install moment --save # bower (deprecated)

Import it to your component

import moment from 'moment'

moment isSame method syntax,

moment().isSame(Moment|String|Number|Date|Array);
moment().isSame(Moment|String|Number|Date|Array, String);

Check if a moment is the same as another moment. The first argument will be parsed as a moment, if not already so.

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

dateVal is the date that you are going to check with in the current week , month or year.

Eg: Assume that if you want to check '20-5-2021' is in the current week, month and year. You can check like this.

moment('20-5-2021').isSame(new Date(), 'week');
moment('20-5-2021').isSame(new Date(), 'month'); 
moment('20-5-2021').isSame(new Date(), 'year'); 
like image 183
Codemaker Avatar answered Oct 12 '22 01:10

Codemaker


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
like image 27
Luca De Nardi Avatar answered Oct 11 '22 23:10

Luca De Nardi


Use .isSame()

Check if a moment is the same as another moment.

moment().isSame(Moment|String|Number|Date|Array); moment().isSame(Moment|String|Number|Date|Array, String);

Supported units: year month week day hour minute second

moment('2010-10-20').isSame('2010-01-01', 'year');  // true
moment('2010-01-01').isSame('2011-01-01', 'month'); // false, different year
moment('2010-01-01').isSame('2010-02-01', 'day');   // false, different month
like image 8
Andreas Avatar answered Oct 12 '22 00:10

Andreas