Determine whether the selected date is within 30 days of the current date, and the selected date is greater than the current date

// 获取用户选择的日期
const selectedDate = new Date('2023-05-01'); // 这里你可以替换成用户选择的日期

// 获取当前日期
const currentDate = new Date();

// 计算两个日期的差值(毫秒)
const diff = Math.abs(selectedDate - currentDate);

// 计算30天的毫秒数
const thirtyDays = 30 * 24 * 60 * 60 * 1000; // 30天,即30 * 24 * 60 * 60 * 1000毫秒

// 如果选中的日期在今天的30天内,则diff小于等于30天的毫秒数
if (diff <= thirtyDays) {
  console.log('用户选择的日期在今天的30天内');
} else {
  console.log('用户选择的日期不在今天的30天内');
}
// 获取当前日期
let currentDate = new Date();

// 获取用户选择的日期
let selectedDate = new Date('2023-05-01'); // 这里你可以替换成用户选择的日期

// 比较两个日期
if (selectedDate > currentDate) {
  console.log('用户选择的日期大于当前日期');
} else {
  console.log('用户选择的日期小于或等于当前日期');
}

Guess you like

Origin blog.csdn.net/aaa123aaasqw/article/details/132305337