我从当前日期减去时间时遇到问题。我的代码看起来像:
var d = new Date(),
year = d.getUTCFullYear(),
month = ('0'+(d.getUTCMonth()+1)).slice(-2),
day = ('0'+d.getUTCDate()).slice(-2),
hour = ('0'+d.getUTCHours()).slice(-2),
minute = ('0'+d.getUTCMinutes()).slice(-2),
second = ('0'+d.getUTCSeconds()).slice(-2);
var startDate = year+'/'+month+'/'+day+'-'+hour+':'+minute+':'+second;
console.log(startDate);
答案 0 :(得分:3)
获取日期变量的毫秒数,减去5分钟并从中创建一个新的日期对象:
var d = new Date()
// d = Mon Feb 29 2016 08:00:09 GMT+0100 (W. Europe Standard Time)
var milliseconds = Date.parse(d)
// 1456729209000
milliseconds = milliseconds - (5 * 60 * 1000)
// - 5 minutes
d = new Date(milliseconds)
// d = Mon Feb 29 2016 07:55:04 GMT+0100 (W. Europe Standard Time)
答案 1 :(得分:1)
答案 2 :(得分:0)
你可以简单地通过
减去 minute = ('0'+d.getUTCMinutes()).slice(-2)-5
var d = new Date(),
year = d.getUTCFullYear(),
month = ('0'+(d.getUTCMonth()+1)).slice(-2),
day = ('0'+d.getUTCDate()).slice(-2),
hour = ('0'+d.getUTCHours()).slice(-2),
minute = ('0'+d.getUTCMinutes()).slice(-2),
second = ('0'+d.getUTCSeconds()).slice(-2);
if (minute>=5)
minute = minute-5;
else {
minute = (parseInt(minute) + 60) - 5;
hour = hour - 1;
}
var startDate = year+'/'+month+'/'+day+'-'+hour+':'+minute+':'+second;
alert(startDate);
答案 3 :(得分:0)
你可以这样使用
var original = new Date();
var subtract5min = new Date();
alert("before : " + original);
subtract5min.setTime(original.getTime() - 5*60*1000);
alert("after : " + subtract5min);

答案 4 :(得分:0)
您可以像下面这样简单地使用
var fiveMinuteAgo = new Date( Date.now() - 1000 * (60 * 5) )