中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
判断两个日期是同一周内的日期。
微信扫码分享
/** * 判断两个日期是否是同一周 */ isSameWeek(date1: number | string | Date, date2: number | string | Date) { date1 = this.getFormatDate(date1); date2 = this.getFormatDate(date2); let blSameYear = date1.getFullYear() == date2.getFullYear(); let blSameMonth = date1.getMonth() == date2.getMonth(); let blSameWeek = date1.getDay() == date2.getDay(); return blSameYear && blSameMonth && blSameWeek; } getFormatDate(date: number | string | Date | undefined): Date { if (date == undefined || date == null) { //无参数 date = new Date(); } else if (typeof (date) == "string") { //字符串日期 if (date.length == 0) { date = new Date(); } else { let dateStr = date.replaceAll("-", '/') .replace("年", '/') .replace("月", '/') .replace("日", ' ') .replace(" ", ' ') .replace("时", ':') .replace("分", ':') .replace("秒", '') date = new Date(dateStr); } } else if (typeof (date) == "number") { //时间戳 date = new Date(date); } else { date = new Date(date); } return date; }