中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
如何判断判断两个日期是否是同一月
微信扫码分享
/** * 判断两个日期是否是同一月 */ isSameMonth(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(); return blSameYear && blSameMonth; } 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; }