如何通过星期几提前获得最近的日期?
How to get nearest date ahead by day of week number?
我是新手,我需要提前一周中的第几天获得最近的日期。
根据当前日期,我需要获取,例如:
const currentWeekDay = '4'; (14.04)
const preparedAlternativeDates = [
{ DAY: 1, VALUE: '18.04' },
{ DAY: 2, VALUE: '19.04'},
{ DAY: 3, VALUE: '20.04'},
{ DAY: 4, VALUE: '21.04'},
{ DAY: 5, VALUE: '15.04'},
{ DAY: 6, VALUE: '16.04'},
{ DAY: 7, VALUE: '17.04'},
];
我正在尝试使用下面的脚本,但它不起作用。也许有更简单的方法来做到这一点?
const ALTERNATIVE_DATES = Object.freeze([
{ DAY: 1, VALUE: null },
{ DAY: 2, VALUE: null },
{ DAY: 3, VALUE: null },
{ DAY: 4, VALUE: null },
{ DAY: 5, VALUE: null },
{ DAY: 6, VALUE: null },
{ DAY: 7, VALUE: null },
]);
const preparedAlternativeDates = ALTERNATIVE_DATES.map((alternativeDate: any) => {
if (!alternativeDate.VALUE) {
const alternativeDateDay = Number(alternativeDate.DAY);
const currentWeekDay = Number(moment().isoWeekday());
if (alternativeDateDay <= currentWeekDay) {
alternativeDate.VALUE = moment().add(alternativeDateDay + currentWeekDay - 1, 'd').startOf('day');
} else {
alternativeDate.VALUE = moment().add(alternativeDateDay - currentWeekDay, 'd').startOf('day');
}
}
return {
day: alternativeDate.DAY,
value: alternativeDate.VALUE
};
});
如有任何帮助,我将不胜感激。
我刚刚调整了你的代码并测试了结果。这应该适合你:
const ALTERNATIVE_DATES = Object.freeze([
{ DAY: 1, VALUE: null },
{ DAY: 2, VALUE: null },
{ DAY: 3, VALUE: null },
{ DAY: 4, VALUE: null },
{ DAY: 5, VALUE: null },
{ DAY: 6, VALUE: null },
{ DAY: 7, VALUE: null },
]);
const preparedAlternativeDates = ALTERNATIVE_DATES.map((alternativeDate: any) => {
if (!alternativeDate.VALUE) {
const wishedDay = Number(alternativeDate.DAY);
const today = moment().isoWeekday();
if (today < wishedDay) {
alternativeDate.VALUE = moment().isoWeekday(wishedDay).format('DD.MM');
} else {
alternativeDate.VALUE = moment().add(1, 'weeks').isoWeekday(wishedDay).format('DD.MM');
}
}
return {
day: alternativeDate.DAY,
value: alternativeDate.VALUE,
};
});
我是新手,我需要提前一周中的第几天获得最近的日期。
根据当前日期,我需要获取,例如:
const currentWeekDay = '4'; (14.04)
const preparedAlternativeDates = [
{ DAY: 1, VALUE: '18.04' },
{ DAY: 2, VALUE: '19.04'},
{ DAY: 3, VALUE: '20.04'},
{ DAY: 4, VALUE: '21.04'},
{ DAY: 5, VALUE: '15.04'},
{ DAY: 6, VALUE: '16.04'},
{ DAY: 7, VALUE: '17.04'},
];
我正在尝试使用下面的脚本,但它不起作用。也许有更简单的方法来做到这一点?
const ALTERNATIVE_DATES = Object.freeze([
{ DAY: 1, VALUE: null },
{ DAY: 2, VALUE: null },
{ DAY: 3, VALUE: null },
{ DAY: 4, VALUE: null },
{ DAY: 5, VALUE: null },
{ DAY: 6, VALUE: null },
{ DAY: 7, VALUE: null },
]);
const preparedAlternativeDates = ALTERNATIVE_DATES.map((alternativeDate: any) => {
if (!alternativeDate.VALUE) {
const alternativeDateDay = Number(alternativeDate.DAY);
const currentWeekDay = Number(moment().isoWeekday());
if (alternativeDateDay <= currentWeekDay) {
alternativeDate.VALUE = moment().add(alternativeDateDay + currentWeekDay - 1, 'd').startOf('day');
} else {
alternativeDate.VALUE = moment().add(alternativeDateDay - currentWeekDay, 'd').startOf('day');
}
}
return {
day: alternativeDate.DAY,
value: alternativeDate.VALUE
};
});
如有任何帮助,我将不胜感激。
我刚刚调整了你的代码并测试了结果。这应该适合你:
const ALTERNATIVE_DATES = Object.freeze([
{ DAY: 1, VALUE: null },
{ DAY: 2, VALUE: null },
{ DAY: 3, VALUE: null },
{ DAY: 4, VALUE: null },
{ DAY: 5, VALUE: null },
{ DAY: 6, VALUE: null },
{ DAY: 7, VALUE: null },
]);
const preparedAlternativeDates = ALTERNATIVE_DATES.map((alternativeDate: any) => {
if (!alternativeDate.VALUE) {
const wishedDay = Number(alternativeDate.DAY);
const today = moment().isoWeekday();
if (today < wishedDay) {
alternativeDate.VALUE = moment().isoWeekday(wishedDay).format('DD.MM');
} else {
alternativeDate.VALUE = moment().add(1, 'weeks').isoWeekday(wishedDay).format('DD.MM');
}
}
return {
day: alternativeDate.DAY,
value: alternativeDate.VALUE,
};
});