我怎样才能将这个伪代码转换成可用的 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());
最佳答案
一旦你知道这一点:
- 您可以通过调用 constructor 创建一个
日期
自 1970 年 1 月 1 日以来以毫秒计。 valueOf()
Date
是自 1970 年 1 月 1 日以来的毫秒数- 一分钟有
60,000
毫秒 :-]
在下面的代码中,通过从 myEndDateTime
中减去适当的毫秒数来创建一个新的 Date
:
var MS_PER_MINUTE = 60000;
var myStartDate = new Date(myEndDateTime - durationInMinutes * MS_PER_MINUTE);
关于javascript - 如何从 JavaScript 中的日期中减去分钟数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/674721/