<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>根据当前时间+初定义间隔5分钟 时间间隔---是15的倍数</title> </head> <body> <div> 当前时间 10:55 间隔 30分钟 营业时间 08:00 - 18:20 列表: 11:25 11:30 11:45 12:00 ... ... 18:00 18:15 </div> <script> // 14:43 23:50 let nowTime = '2023-02-06 23:36'; //下单时间 let jiange = 5; //间隔 let startTime = '2023-02-06 14:43'; //设置开始时间 let endTime = '2023-02-06 23:50'; //设置结束时间 let arr = [] let first = getDateNum(nowTime) + jiange * 60 * 1000 arr.push(getTime(first)) //获取下一个时间15分钟整点 let s = nowTime.substring(11, 13) let f = nowTime.substring(14, 16) if (f <= 15) { f = '15' } else if (f <= 30) { f = '30' } else if (f <= 45) { f = '45' } else { f = '00' s = +s + 1 } let 整点时间 = nowTime.substring(0, 11) + s + ':' + f //new Date(endTime.replace(/-/g, "/")-处理了ios手机时间兼容(可替换掉-getDateNum(endTime) let 剩余营业分钟 = (getDateNum(endTime) - getDateNum(nowTime)) / 1000 / 60 let 剩余数量 = 剩余营业分钟 / 15 for (var i = 0; i < 剩余数量; i++) { let time = getDateNum(整点时间) + i * 15 * 60 * 1000 if (time >= first) { arr.push(getTime(time)) } } console.log(arr); function getDateNum(s) { // .replace(/-/g, "/") 处理了ios手机时间兼容 return new Date(s.replace(/-/g, "/")).getTime() } function getTime(date) { date = new Date(date) let Y = date.getFullYear(), M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1), D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()), h = (date.getHours() < 10 ? '0' + (date.getHours()) : date.getHours()), m = (date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes()), s = (date.getSeconds() < 10 ? '0' + (date.getSeconds()) : date.getSeconds()); return Y + '-' + M + '-' + D + ' ' + h + ':' + m + ':' + s } </script> </body> </html>