Khanh's blog

<- Quay về trang chủ

Date manipulation with moment.js

In my recent project, I have a filter as the image bellow:

Screen Shot 2015-12-25 at 11.16.17 AM

The period dropdown field have some options:

and when you change the Period, the Date From and Date To should be changed with corresponding data.

For example, let's say today is 25/12/2015. So:

If you implement this function by pure Javascript you may have some difficulty such as how to get the last day of specific month, how to get the begin & end date of a week...

Fortunately, we have moment.js library which help us manipulate with datetime in Javascript very easy.

The above requirements can be done by small line of codes (in my case, I use AngularJS)

Much easier, right?