【发布时间】:2018-01-31 22:37:40
【问题描述】:
我需要显示距离截止日期还有多远,即每周五下午 5 点。 Moment 将为我提供一周中某一天的日期,但它始终是当前时间,因此截止日期始终显示“x 天,23 小时,59 分钟”。我如何得到这一天,以及从某一刻起的特定时间?在我的示例中,我需要截止日期为“下周五,17:00”而不是“下周五,当前时间”
console.log(timeLeft());
function timeLeft() {
var dayINeed = 5; // for Friday
var deadline;
// if we haven't yet passed the day of the week that I need:
if (moment().isoWeekday() <= dayINeed) {
// then just give me this week's instance of that day
deadline = moment().isoWeekday(dayINeed);
} else {
// otherwise, give me next week's instance of that day
deadline = moment().add(1, 'weeks').isoWeekday(dayINeed);
}
console.log(deadline);
const now = moment();
const days = deadline.diff(now, 'days');
const hours = deadline.subtract(days, 'days').diff(now, 'hours');
const minutes = deadline.subtract(hours, 'hours').diff(now, 'minutes');
return `${days} days, ${hours} hours, and ${minutes} minutes`;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.js"></script>
【问题讨论】:
标签: momentjs