最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get days left till end of the month with moment.js - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question asked Feb 17, 2015 at 16:56 0NLY7770NLY777 3131 gold badge4 silver badges10 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 11

You could do something like this:

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

if(a.diff(b, 'days') <= 13)
{
    //do something
}

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();
}

Maybe something like this can help.

const d = moment();
const currentDay = d.get("date");
const daysInMonth = d.daysInMonth();
const remainingDays = daysInMonth - currentDay;

console.log(remainingDays <= 13)
发布评论

评论列表(0)

  1. 暂无评论