3 回答
TA贡献2051条经验 获得超10个赞
以防万一有人正在寻找一种解决方案来实现相同的目标而不使用 Momentjs(现在正式处于遗留模式):
// Set two date objects 30 minutes apart
const [currentMessage,previousMessage] = [new Date(), new Date()]
previousMessage.setMinutes(previousMessage.getMinutes() - 30 )
// Get difference by subtracting milliseconds and converting to minutes
const diffInMinutes = (currentMessage.getTime()- previousMessage.getTime())/60000
console.log('difference is:',diffInMinutes)
TA贡献1872条经验 获得超3个赞
让我们这样说:
var currentMessage = moment(new Date()); //todays time
var previousMessage = moment("2018-12-1"); // another time
var duration = moment.duration(currentMessage.diff(previousMessage));
var days = duration.asDays(); // you can choose days, minutes or whatever you need
请注意,在减法之前不要对它们进行格式化,之后您可以将逻辑用于标志,或者可以将其用作标记标签的函数。我希望这会帮助你。
TA贡献1784条经验 获得超9个赞
使用加法或减法
let currentMessage = moment
.utc(conversationMessages[this.index].createdDate)
let previousMessage = moment
.utc(conversationMessages[this.index - 1].createdDate)
if( currentMessage > previousMessage.add(15, 'minutes') ) {
// do your thing
}
添加回答
举报