moment js判断已经过了多少天,几周
Moment js determine how many days, weeks has passed
我不太擅长处理 日期时间。我将 momentjs 用于 ionic 应用程序 来操纵时间,但我想实现我无法实现的目标。
我为此使用了一个管道,我想根据已经过去了多少天或者几周、几个月或几年来显示。使用 相对时间 会帮助我,就像 fromNow()
方法和 momentjs 的 calendar()
一样。但就我而言,我会有多个 conditions
.
下面是我的管道的示例代码
transform(value: Date | moment.Moment, dateFormat: string): any {
if (moment(value) < moment(value).subtract(7, 'days')) {
return moment(value).format('llll') // Use this format if weeks, months or years has passed
} else if (moment(value) < moment(value).subtract(1, 'days')) {
return moment(value).calendar(); // Use calendar time if 1 day has passed
} else {
return moment(value).fromNow(); // Use relative time if within 24 hours
}
}
如果秒、分钟或小时过去了 24 小时,我将使用 fromNow()
方法,但当几天过去时,我将使用 calendar()
,如果几周、几个月或几年过去了,请使用此 format('llll')
.
有人能给我一些启发吗?
提前致谢。
据我了解,您想根据多长时间前某个特定时刻来自now
来做出决定。您似乎有 3 种情况:> 7 天,> 1 天,<1 天。
Momentjs 提供了一个非常有用的diff
方法。所以,你可以这样做:
var currDate = moment.now();
var dateToTest = moment(val);
// if dateToTest will always be in past, use currDate as the base to diff, else
be prepared to handle the negative outcomes.
var result = currDate.diff(dateToTest, 'days')
window.onload = function() {
console.log("Test Cases: ")
console.log("Input: Date is 2 minutes behind")
dateThing("2018-07-20T12:02:54+00:00");
console.log("Input: Date is few hours behind")
dateThing("2018-07-20T07:02:54+00:00");
console.log("Input: Date is 23 hours 59 minutes behind")
dateThing("2018-07-19T12:03:54+00:00");
console.log("Input: Date is 24 hours behind")
dateThing("2018-07-19T12:04:54+00:00");
console.log("Input: Date is 2 days behind")
dateThing("2018-07-18T12:04:54+00:00");
console.log("Input: Date is 12 days behind")
dateThing("2018-07-08T12:04:54+00:00");
}
dateThing = function(val) {
// for now freezing the "now" so that precise testcases can be written.
// var currDate = moment.now();
var currDate = moment("2018-07-20T12:04:54+00:00")
var dateToTest = moment(val);
// if dateToTest will always be in past, use currDate as the base to diff, else be prepared to handle the negative outcomes.
var result = currDate.diff(dateToTest, 'days')
if (result > 7) {
console.log("Output: date is more than 1 week behind")
} else if (result > 1) {
console.log("Output: date is more than 1 day but less than 1 week behind")
} else {
console.log("Output: date is less than 1 day behind")
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
请运行上面的代码片段来查看边界情况的行为,如果不准确,您可以分分钟进行差异化并反转流程。
给你。 :)
function transformDate(givenDate) {
if (moment().subtract(7, 'days').valueOf() > moment(givenDate).valueOf()) {
return moment(givenDate).format('llll');
} else if (moment().subtract(1, 'days').valueOf() > moment(givenDate).valueOf()) {
return moment(givenDate).calendar();
} else {
return moment(givenDate).fromNow();
}
}
// current time
console.log(transformDate(new Date()));
// 8 days before this answer was posted
console.log(transformDate(new Date(1531394211385)));
// 2 days before this answer was posted
console.log(transformDate(new Date(1531912692803)));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>
我不太擅长处理 日期时间。我将 momentjs 用于 ionic 应用程序 来操纵时间,但我想实现我无法实现的目标。
我为此使用了一个管道,我想根据已经过去了多少天或者几周、几个月或几年来显示。使用 相对时间 会帮助我,就像 fromNow()
方法和 momentjs 的 calendar()
一样。但就我而言,我会有多个 conditions
.
下面是我的管道的示例代码
transform(value: Date | moment.Moment, dateFormat: string): any {
if (moment(value) < moment(value).subtract(7, 'days')) {
return moment(value).format('llll') // Use this format if weeks, months or years has passed
} else if (moment(value) < moment(value).subtract(1, 'days')) {
return moment(value).calendar(); // Use calendar time if 1 day has passed
} else {
return moment(value).fromNow(); // Use relative time if within 24 hours
}
}
如果秒、分钟或小时过去了 24 小时,我将使用 fromNow()
方法,但当几天过去时,我将使用 calendar()
,如果几周、几个月或几年过去了,请使用此 format('llll')
.
有人能给我一些启发吗?
提前致谢。
据我了解,您想根据多长时间前某个特定时刻来自now
来做出决定。您似乎有 3 种情况:> 7 天,> 1 天,<1 天。
Momentjs 提供了一个非常有用的diff
方法。所以,你可以这样做:
var currDate = moment.now();
var dateToTest = moment(val);
// if dateToTest will always be in past, use currDate as the base to diff, else
be prepared to handle the negative outcomes.
var result = currDate.diff(dateToTest, 'days')
window.onload = function() {
console.log("Test Cases: ")
console.log("Input: Date is 2 minutes behind")
dateThing("2018-07-20T12:02:54+00:00");
console.log("Input: Date is few hours behind")
dateThing("2018-07-20T07:02:54+00:00");
console.log("Input: Date is 23 hours 59 minutes behind")
dateThing("2018-07-19T12:03:54+00:00");
console.log("Input: Date is 24 hours behind")
dateThing("2018-07-19T12:04:54+00:00");
console.log("Input: Date is 2 days behind")
dateThing("2018-07-18T12:04:54+00:00");
console.log("Input: Date is 12 days behind")
dateThing("2018-07-08T12:04:54+00:00");
}
dateThing = function(val) {
// for now freezing the "now" so that precise testcases can be written.
// var currDate = moment.now();
var currDate = moment("2018-07-20T12:04:54+00:00")
var dateToTest = moment(val);
// if dateToTest will always be in past, use currDate as the base to diff, else be prepared to handle the negative outcomes.
var result = currDate.diff(dateToTest, 'days')
if (result > 7) {
console.log("Output: date is more than 1 week behind")
} else if (result > 1) {
console.log("Output: date is more than 1 day but less than 1 week behind")
} else {
console.log("Output: date is less than 1 day behind")
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
请运行上面的代码片段来查看边界情况的行为,如果不准确,您可以分分钟进行差异化并反转流程。
给你。 :)
function transformDate(givenDate) {
if (moment().subtract(7, 'days').valueOf() > moment(givenDate).valueOf()) {
return moment(givenDate).format('llll');
} else if (moment().subtract(1, 'days').valueOf() > moment(givenDate).valueOf()) {
return moment(givenDate).calendar();
} else {
return moment(givenDate).fromNow();
}
}
// current time
console.log(transformDate(new Date()));
// 8 days before this answer was posted
console.log(transformDate(new Date(1531394211385)));
// 2 days before this answer was posted
console.log(transformDate(new Date(1531912692803)));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>