[英]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.我想使用moment.js 根据本月是否还剩不到2 周来显示或隐藏链接,但我不确定正确的方法。
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.
提前致谢。
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:如果你正在寻找一个普通的 javascript 版本,我已经写了这个函数:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.