我从 php 中获取了 2 个字符串,并尝试使用 javascript 来获取两者之间的区别。这是我的代码:
this.now = Thu Feb 14 2017 16:38:42 GMT-0200 (BRST)
this.expiration = Wed Feb 15 2017 15:29:45 GMT-0200 (BRST)
我想要这两个日期之间的天数差异,以显示类似 "1" day left
的内容.
最佳答案
有几种方法可以做到这一点:
1) 可以使用像 [ date-fns#differenceInDays 这样的库]
2)编写简单的 diff 函数
const msInDay = 24 * 3600 * 1000
function diffInDays(startDateString, endDateString) {
const start = new Date(startDateString)
const end = new Date(endDateString)
const ms = end - start
const fullDays = Math.round(ms / msInDay)
return fullDays
}
关于javascript - 使用javascript获取两个字符串之间的日差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42256348/