从给定日期计算 n 分钟后的日期,返回其 字符串 表示形式。
使用 Date 构造函数从第一个参数创建 Date 对象。
使用 Date. prototype .getTime() 和 Date.prototype.setTime() 将 n 分钟添加到给定日期。
使用 Date.prototype.toISOString()、 String .prototype.split() 和 String.prototype.replace() 方法返回 yyyy-mm-dd HH:MM:SS 格式的时间字符串。
JavaScript
const addMinutesToDate = (date, n) => { const d = new Date(date); d.setTime(d.getTime() + n * 60000); return d.toISOString().split('.')[0].replace('T',' ');};
示例:
addMinutesToDate('2020-10-19 12:00:00', 10); // '2020-10-19 12:10:00'addMinutesToDate('2020-10-19', -10); // '2020-10-18 23:50:00'
更多内容请访问我的网站: