Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get days left till end of the month with moment.js

I want to display or hide a link depending on whether there are less than 2 weeks left in the month, using moment.js, but I'm not sure the correct way to go about it.

Currently I have...

if (moment().endOf('month')<=(13, 'days'))
{
    //do link stuff here
}

...but I don't think that's the correct way of doing it. It certainly isn't doing anything anyway. Could anyone give me any pointers? Thanks in advance.

like image 638
0NLY777 Avatar asked Feb 17 '15 16:02

0NLY777


2 Answers

You could do something like this:

var a = moment().endOf('month');
var b = moment();

if(a.diff(b, 'days') <= 13)
{
    //do something
}
like image 114
ThreadedLemon Avatar answered Oct 22 '22 08:10

ThreadedLemon


If you're looking for a plain javascript version, I've wrote this function:

function getMonthDaysLeft(){
    date = new Date();
    return new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate() - date.getDate();
}
like image 4
CIRCLE Avatar answered Oct 22 '22 09:10

CIRCLE