/* * 日期解析,字符串转日期 */function dateParse(dateString) { var SEPARATOR_BAR = "-"; var SEPARATOR_SLASH = "/"; var SEPARATOR_DOT = "."; var dateArray; if (dateString.indexOf(SEPARATOR_BAR) > -1) { dateArray = dateString.split(SEPARATOR_BAR); } else if (dateString.indexOf(SEPARATOR_SLASH) > -1) { dateArray = dateString.split(SEPARATOR_SLASH); } else { dateArray = dateString.split(SEPARATOR_DOT); } return new Date(dateArray[0], dateArray[1] - 1, dateArray[2]);}; /** * 日期比较大小 * compareDateString大于dateString,返回1; * 等于返回0; * compareDateString小于dateString,返回-1 */ function dateCompare(dateString, compareDateString) { var dateTime = dateParse(dateString).getTime(); if(compareDateTime != null){ var compareDateTime = dateParse(compareDateString).getTime();
} if (compareDateTime > dateTime) { return 1; } else if (compareDateTime == dateTime) { return 0; } else { return -1; }}; /** * 判断日期是否在区间内,在区间内返回true,否返回false */ function isDateBetween(dateString, startDateString, endDateString) { if (startDateString == null && endDateString == null) { return false; } var flag = false; var startFlag = (dateCompare(dateString, startDateString) < 1); if (startFlag && endDateString == null) { return true; } var endFlag = (dateCompare(dateString, endDateString) > -1); if (startFlag && endFlag) { flag = true; } return flag;}; 可以传入开始时间和结束时间,也可以只传入开始时间。
原文地址:https://www.cnblogs.com/yang-xiansen/p/10172066.html
时间: 2024-10-15 11:07:48