如何在每周的一天中提前到达最近的日期?

发布于 2025-01-21 09:37:45 字数 1285 浏览 0 评论 0原文

我是新手,我需要在一周中的一天之前获得最近的日期。

根据当前日期,我需要获取:例如:

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
  };
});

我将感谢任何帮助。

I'm new to moment and I need to get the nearest date ahead by the number of the day of the week.

Depending on the current date, I need to get, for example:

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'},
];

I'm trying to use the script below but it doesn't work. Maybe there is an easier way to do this?

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
  };
});

I will be grateful for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把回忆走一遍 2025-01-28 09:37:45

我只是调整了您的代码并测试了结果。这应该对您有用:

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,
     };
  });

”在此处输入图像描述”

I just adjusted your code and tested the result. this should work for you:

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,
     };
  });

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文