如何用 JavaScript 从日期中减去分钟?

如何将这些伪代码转换为可用的 JS (不要担心结束日期来自哪里,除非它是一个有效的 JavaScript 日期)。

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 - durationInMinutes;


alert("The event will start on " + myStartDate.toDateString() + " at " + myStartDate.toTimeString());
214717 次浏览

一旦你知道了这一点:

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

在下面的代码中,通过从 myEndDateTime中减去适当的毫秒数来创建一个新的 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);

这就是我所做的: 看看 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;
}
}

这是我发现的:

//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

Js 有一些非常方便的方法来操作日期对象

。减去方法允许您通过提供数量和时间单位字符串,从一个日期中减去一定数量的时间单位。

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 ...

Luxon 还有一个针对 操纵的 API,这是它自己的 DateTime对象

var dt = DateTime.now();
// "1982-05-25T00:00:00.000Z"
dt.minus({ minutes: 3 });
dt.toISO();
// "1982-05-24T23:57:00.000Z"

只是些虱子

刻度表示自1970年1月1日0:0:0协调世界时以来的毫秒。日期构造函数可以将一个数字作为单个参数,该参数被解释为刻度。

当处理毫秒/秒/小时/天/周(静态量)的数量时,您可以这样做

const aMinuteAgo = new Date( Date.now() - 1000 * 60 );

或者

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

然后,让 JavaScript 显示日期,并担心它是一周中的哪一天,或者哪一天的月份和年份等等。您可以选择将其本地化,也可以使用 英特尔将其本地化为国际化。

我建议在做任何比上面提到的更复杂的事情时,如果需要任意的时区、闰年或每月的天数等,可以使用 卢森进行与 JavaScript 相关的项目。

使用此函数扩展 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;
};

因此,你可以添加或减去一些分钟,秒,小时,天... 到任何日期。

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().2将 Unix 时间戳转换为常规时间

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

尝试如下:

var dt = new Date();
dt.setMinutes( dt.getMinutes() - 20 );
console.log('#####',dt);