在当前日期之前获取特定日期

时间:2020-10-02 16:55:59

标签: javascript date

如何使用JavaScript从一个月的基准日获取最新日期。

我在任何地方都找不到它,所以让我在一个示例中进行解释:

今天是10月2日,因为我的基准日是5,所以我需要以某种方式检索9月5日的日期。日期应该是过去的日期,这意味着如果当前日期是10月6日,则我需要返回10月5日,如果是11月6日,则返回11月5日,如果是11月23日,则返回11月5日,依此类推。

我希望我足够清楚。

5 个答案:

答案 0 :(得分:1)

如果给定“不可能”的日期(如-1个月),则<v-snackbar centered /> 构造函数很聪明,可以滚动到前几个月。请注意,JavaScript日期中的月份从零开始,因此0是一月,11是十二月,依此类推。

Date

答案 1 :(得分:0)

希望我能正确理解

stringDate参数是可选的,因此您可以测试10/6/20

${currentMonth + 1}是因为索引的月份为0。当您将它们传递回Date构造函数时,它们将以面值9 = Sept进行解释。

function getPastDate(stringDate) {
    const currentDate = stringDate ? new Date(stringDate) : new Date();
    const currentDay = currentDate.getDate();
    const currentMonth = currentDate.getMonth(); // 9 for Oct Months are 0 indexed
    const currentYear = currentDate.getFullYear();
    const baseDay = 5;
    console.log(currentDay, currentMonth, currentYear)
    if (currentDay > baseDay) {
        return new Date(`${currentMonth + 1}/${baseDay}/${currentYear}`);
    }

    return new Date(`${currentMonth}/${baseDay}/${currentYear}`);
}

答案 2 :(得分:0)

这是可以满足您要求的东西。它没有错误检查功能,因此当基数小于或大于当月或当月的天数时,它将给出时髦的结果。

let base = 5;
let date = new Date()
let today = date.getDate();
if (today >= base) {
    console.log(date.toString());
} else {
    let d = date.getMonth()
    date.setMonth(d-1);
  date.setDate(base)
  console.log(date.toString());
}

答案 3 :(得分:0)

我希望我明白到底想要什么。这可能是一个解决方案:

Solution = (base, dd = 0, mm = 0) => {
  let months = [
    ["January", 31],
    ["February", 29],
    ["March", 31],
    ["April", 30],
    ["May", 31],
    ["June", 30],
    ["July", 31],
    ["August", 31],
    ["September", 30],
    ["October", 31],
    ["November", 30],
    ["December", 31],
  ];
  let d = 0;
  let m = 0;
  if (dd === 0 && mm === 0) {
    day = new Date();
    d == day.getDate();
    m = day.getMonth();
  } else if (validDay(dd, mm, months)) {
    d = dd;
    m = mm - 1;
  } else {
    return "invalid date";
  }
  if (d <= base) {
    base = base >= 10 ? base : "0" + base;
    return base + " " + months[m - 1][0];
  } else {
    base = base >= 10 ? base : "0" + base;
    return base + " " + months[m][0];
  }
};
validDay = (day, month, months) => {
  if (month >= 1 && month <= 12) {
    if (day >= 1 && day <= months[month - 1][1]) {
      return true;
    }
    return false;
  }
};

如果未设置mm和dd参数,则只需要今天。 该解决方案不关心年份是否leap年,但很容易检查这一点。 祝你有美好的一天。

答案 4 :(得分:-1)

如果您设置的日期大于当前日期,则必须首先回退月份。

完成此操作后,您可以设置日期。

const dates = [
  { in: '2020-10-05T00:00:00Z', out: '2020-10-05T00:00:00.000Z' },
  { in: '2020-10-06T00:00:00Z', out: '2020-10-05T00:00:00.000Z' },
  { in: '2020-11-06T00:00:00Z', out: '2020-11-05T00:00:00.000Z' },
  { in: '2020-11-23T00:00:00Z', out: '2020-11-05T00:00:00.000Z' },
  { in: '2020-11-04T00:00:00Z', out: '2020-10-05T00:00:00.000Z' },
  { in: '2020-01-01T00:00:00Z', out: '2019-12-05T00:00:00.000Z' }
],
  dayOfMonth = 5;

/**
 *  Takes a UTC date modifies the day of month.
 *  @param {Date} dateObj
 *  @param {int} dayOfMonth
 *  @return Returns a past date for the provided dateObj
 */
const toBaseDate = (dateObj, dayOfMonth) => {
  const [ date, month ] = [ dateObj.getUTCDate(), dateObj.getUTCMonth() ];
  if (date === dayOfMonth) return dateObj;
  if (date < dayOfMonth) dateObj.setUTCMonth(month - 1);
  dateObj.setUTCDate(dayOfMonth);
  return dateObj;
}

console.log(dates.every(dateString => {
  const date = new Date(dateString.in);
  const baseDate = toBaseDate(date, dayOfMonth);
  return baseDate.toISOString() === dateString.out;
}) ? 'pass' : 'fail');
.as-console-wrapper { top: 0; max-height: 100% !important; }

相关问题