如何从JavaScript中的date减去分钟?

我怎样才能把这个伪代码翻译成工作js [不要担心结束date来自哪里,除非它是一个有效的javascriptdate]。

var myEndDateTime = somedate; //somedate is a valid js date var durationInMinutes = 100; //this can be any number of minutes from 1-7200 (5 days) //this is the calculation I don't know how to do var myStartDate = somedate - durationInMuntes; alert("The event will start on " + myStartDate.toDateString() + " at " + myStartDate.toTimeString()); 

一旦你知道这一点:

  • 您可以通过自1970年1月1日起以毫秒为单位调用构造函数来创buildDate
  • valueOf() a Date是1970年1月1日以来的毫秒数
  • 一分钟内有60,000毫秒: – ]

…并不难。

在下面的代码中,通过从myEndDateTime减去适当的毫秒数来创build一个新的Date

 var MS_PER_MINUTE = 60000; var myStartDate = new Date(myEndDateTime - durationInMinutes * MS_PER_MINUTE); 

您也可以使用get和set分钟来实现它:

 var endDate = somedate; var startdate = new Date(endDate); var durationInMinutes = 20; startdate.setMinutes(endDate.getMinutes() - durationInMinutes); 

moment.js有一些不错的方便方法来操作date对象

.subtract方法允许您从date中减去一定数量的时间单位,通过提供金额和timeunitstring。

 var now = new Date(); // Sun Jan 22 2017 17:12:18 GMT+0200 ... var olderDate = moment(now).subtract(3, 'minutes').toDate(); // Sun Jan 22 2017 17:09:18 GMT+0200 ... 

一切都只是蜱,不需要记忆的方法

 var aMinuteAgo = new Date( (new Date()).getTime() - 1000 * 60 ); 

要么

 var aMinuteLess = new Date( someDate.getTime() - 1000 * 60 ); 

这是我发现的:

 //First, start with a particular time var date = new Date(); //Add two hours var dd = date.setHours(date.getHours() + 2); //Go back 3 days var dd = date.setDate(date.getDate() - 3); //One minute ago... var dd = date.setMinutes(date.getMinutes() - 1); //Display the date: var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; var date = new Date(dd); var day = date.getDate(); var monthIndex = date.getMonth(); var year = date.getFullYear(); var displayDate = monthNames[monthIndex] + ' ' + day + ', ' + year; alert('Date is now: ' + displayDate); 

资料来源:

http://www.javascriptcookbook.com/article/Perform-date-manipulations-based-on-adding-or-subtracting-time/

https://stackoverflow.com/a/12798270/1873386

这就是我所做的: 在Codepen上看到

 var somedate = 1473888180593; var myStartDate; //var myStartDate = somedate - durationInMuntes; myStartDate = new Date(dateAfterSubtracted('minutes', 100)); alert("The event will start on " + myStartDate.toDateString() + " at " + myStartDate.toTimeString()); function dateAfterSubtracted(range, amount){ var now = new Date(); if(range === 'years'){ return now.setDate(now.getYear() - amount); } if(range === 'months'){ return now.setDate(now.getMonth() - amount); } if(range === 'days'){ return now.setDate(now.getDate() - amount); } if(range === 'hours'){ return now.setDate(now.getHours() - amount); } if(range === 'minutes'){ return now.setDate(now.getMinutes() - amount); } else { return null; } } 

使用此function扩展date类

 // Add (or substract if value is negative) the value, expresed in timeUnit // to the date and return the new date. Date.dateAdd = function(currentDate, value, timeUnit) { timeUnit = timeUnit.toLowerCase(); var multiplyBy = { w:604800000, d:86400000, h:3600000, m:60000, s:1000 }; var updatedDate = new Date(currentDate.getTime() + multiplyBy[timeUnit] * value); return updatedDate; }; 

因此,您可以添加或减去几分钟,几秒,几小时,几天…到任何date。

 add_10_minutes_to_current_date = Date.dateAdd( Date(), 10, "m"); subs_1_hour_to_a_date = Date.dateAdd( date_value, -1, "h"); 
 var date=new Date(); //here I am using "-30" to subtract 30 minutes from the current time. var minute=date.setMinutes(date.getMinutes()-30); console.log(minute) //it will print the time and date according to the above condition in Unix-timestamp format. 

您可以使用new Date()例如将Unix时间戳转换为常规时间

 var extract=new Date(minute) console.log(minute)//this will print the time in the readable format.