我怎样才能把这个伪代码翻译成工作的 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());
一旦你知道这一点:
Date
valueOf()
60,000
在下面的代码中,Date通过从 减去适当的毫秒数来创建一个新的myEndDateTime:
myEndDateTime
var MS_PER_MINUTE = 60000; var myStartDate = new Date(myEndDateTime - durationInMinutes * MS_PER_MINUTE);