JS判断当前日期是否为月末(全网最简单)

思路:每个月的月末不一定,但是每个月的月初一定是 1 号,当前日期加 1 日,如果是 1 号就是月末

/**
 * 判断当前日期是否是该月的最后一天,即月末判断
 * @param date
 * @returns
 */
export const isLastDateOfCurrentMonth = (dateData: Date) => {
    
    
  if (!dateData) return false;
  const date: Date = new Date(dateData);
  const next_date = new Date(date?.getTime() + 24 * 60 * 60 * 1000)?.getDate();
  if (next_date === 1) {
    
    
    return true;
  }
  return false;
};

其他: 获取当前日期的前几天,或后几天

// 获取当前日期的前几天,或后几天
const currentDate = new Date();
const oneDay = 24 * 60 * 60 * 1000;
const lastDate = new Date(currentDate?.getTime() - oneDay);
const nextDate = new Date(currentDate?.getTime() + oneDay);

猜你喜欢

转载自blog.csdn.net/yiguoxiaohai/article/details/127693734
今日推荐