如何将此伪代码转换为工作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 - durationInMuntes;
alert("The event will start on " + myStartDate.toDateString() + " at " + myStartDate.toTimeString());
答案 0 :(得分:173)
一旦你知道这一点:
Date
。valueOf()
a Date
是自1970年1月1日以来的毫秒数60,000
毫秒: - ] ......不是那么难。
在下面的代码中,通过从Date
减去适当的毫秒数来创建新的myEndDateTime
:
var MS_PER_MINUTE = 60000;
var myStartDate = new Date(myEndDateTime - durationInMinutes * MS_PER_MINUTE);
答案 1 :(得分:67)
您还可以使用get和set分钟来实现它:
var endDate = somedate;
var startdate = new Date(endDate);
var durationInMinutes = 20;
startdate.setMinutes(endDate.getMinutes() - durationInMinutes);
答案 2 :(得分:23)
一切都只是滴答,不需要记住方法 ......
var aMinuteAgo = new Date( Date.now() - 1000 * 60 );
或
var aMinuteLess = new Date( someDate.getTime() - 1000 * 60 );
在使用momentjs之后,我不得不说这是一个很棒的图书馆,你应该看看。确实,在许多情况下,刻度线可以使您的代码非常小,并且您应该尝试使您的代码尽可能小,以满足您的需要。但对于任何复杂的事情,请使用momentjs。
答案 3 :(得分:9)
moment.js有一些非常好的方便方法来操作日期对象
.subtract方法允许您通过提供金额和时间单位字符串从日期中减去一定数量的时间单位。
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 ...
答案 4 :(得分:5)
这是我发现的:
//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);
来源:
答案 5 :(得分:2)
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.
答案 6 :(得分:0)
这就是我所做的:see on 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;
}
}
答案 7 :(得分:0)
使用此函数扩展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");
答案 8 :(得分:0)
尝试如下:
var dt = new Date();
dt.setMinutes( dt.getMinutes() - 20 );
console.log('#####',dt);