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