如何在Javascript中操作当前时间(多次并将其添加到表中)?
我想做什么: 我想反复更改当前时间,并在HTML文档的不同表行中显示它们 示例:如何在Javascript中操作当前时间(多次并将其添加到表中)?,javascript,jquery,html,Javascript,Jquery,Html,我想做什么: 我想反复更改当前时间,并在HTML文档的不同表行中显示它们 示例: var deductions = [10, 20, 60, 71]; //Set your deductions here window.setInterval(showpasttime(), 1000); function showpasttime(){ for(i=0;i<deductions.length;i++){ //Iterate through the time deductions i
var deductions = [10, 20, 60, 71]; //Set your deductions here
window.setInterval(showpasttime(), 1000);
function showpasttime(){
for(i=0;i<deductions.length;i++){ //Iterate through the time deductions in the array
d = new Date ();
d.setMinutes(d.getMinutes()-deductions[i]); //gives me current minutes MINUS specified number of minutes minutes
month = d.getMonth () + 1;
h = (d.getHours () < 10 ? '0' + d.getHours () : d.getHours ());
m = (d.getMinutes () < 10 ? '0' + d.getMinutes () : d.getMinutes ());
document.getElementById("row"+i).innerHTML = deductions[i]+' minutes ago it was '
+ d.getDate () + '.'
+ month + '.'
+ d.getFullYear () +
' - '
+ h + ':' + m ;
}
};
我想从现在开始计算10、20、60和71分钟前的时间。因此,如果我电脑上的当前时间是23:00(在欧洲),它应该给我22:50、22:40、22:00和21:49
到目前为止我所做的:
var deductions = [10, 20, 60, 71]; //Set your deductions here
window.setInterval(showpasttime(), 1000);
function showpasttime(){
for(i=0;i<deductions.length;i++){ //Iterate through the time deductions in the array
d = new Date ();
d.setMinutes(d.getMinutes()-deductions[i]); //gives me current minutes MINUS specified number of minutes minutes
month = d.getMonth () + 1;
h = (d.getHours () < 10 ? '0' + d.getHours () : d.getHours ());
m = (d.getMinutes () < 10 ? '0' + d.getMinutes () : d.getMinutes ());
document.getElementById("row"+i).innerHTML = deductions[i]+' minutes ago it was '
+ d.getDate () + '.'
+ month + '.'
+ d.getFullYear () +
' - '
+ h + ':' + m ;
}
};
我设法更改了一次时间,但我不知道如何更改几次(可能是使用“for”或“while”-循环?),并将它们全部添加到html文档中的表中
这是我的.js:
window.setInterval("showpasttime()", 1000);
function showpasttime()
{
d = new Date ();
d.setMinutes(d.getMinutes()-10); //gives me current minutes MINUS 10 minutes
month = d.getMonth () + 1;
h = (d.getHours () < 10 ? '0' + d.getHours () : d.getHours ());
m = (d.getMinutes () < 10 ? '0' + d.getMinutes () : d.getMinutes ());
document.getElementById("row2").innerHTML = '10 minutes ago it was '
+ d.getDate () + '.'
+ month + '.'
+ d.getFullYear () +
' - '
+ h + ':' + m ;
};
window.setInterval(“showpasttime()”,1000);
函数showpasttime()
{
d=新日期();
d、 setMinutes(d.getMinutes()-10);//给我当前分钟减去10分钟
月=d.getMonth()+1;
h=(d.getHours()<10?'0'+d.getHours():d.getHours());
m=(d.getMinutes()<10?'0'+d.getMinutes():d.getMinutes());
document.getElementById(“row2”).innerHTML='10分钟前是'
+d.getDate()+'。'
+月份+'。'
+d.getFullYear()+
' - '
+h+':'+m;
};
到目前为止的结果:
var deductions = [10, 20, 60, 71]; //Set your deductions here
window.setInterval(showpasttime(), 1000);
function showpasttime(){
for(i=0;i<deductions.length;i++){ //Iterate through the time deductions in the array
d = new Date ();
d.setMinutes(d.getMinutes()-deductions[i]); //gives me current minutes MINUS specified number of minutes minutes
month = d.getMonth () + 1;
h = (d.getHours () < 10 ? '0' + d.getHours () : d.getHours ());
m = (d.getMinutes () < 10 ? '0' + d.getMinutes () : d.getMinutes ());
document.getElementById("row"+i).innerHTML = deductions[i]+' minutes ago it was '
+ d.getDate () + '.'
+ month + '.'
+ d.getFullYear () +
' - '
+ h + ':' + m ;
}
};
在我的html文档中,“10分钟前的时间是2014年3月5日-22:50”以id=“row2”进入tablerow
问题/问题:
var deductions = [10, 20, 60, 71]; //Set your deductions here
window.setInterval(showpasttime(), 1000);
function showpasttime(){
for(i=0;i<deductions.length;i++){ //Iterate through the time deductions in the array
d = new Date ();
d.setMinutes(d.getMinutes()-deductions[i]); //gives me current minutes MINUS specified number of minutes minutes
month = d.getMonth () + 1;
h = (d.getHours () < 10 ? '0' + d.getHours () : d.getHours ());
m = (d.getMinutes () < 10 ? '0' + d.getMinutes () : d.getMinutes ());
document.getElementById("row"+i).innerHTML = deductions[i]+' minutes ago it was '
+ d.getDate () + '.'
+ month + '.'
+ d.getFullYear () +
' - '
+ h + ':' + m ;
}
};
现在,我想在其他时间段重复这样做(同样是20、60和71分钟前)。我是否需要一次又一次地编写此代码,或者是否有更优雅的方法来完成此操作,例如使用“for”或“while”函数?
非常感谢(我希望这个问题不要太愚蠢,因为我对html/css/javascript/jquery完全陌生) 您可以将时间偏移放在
数组中,并在for
循环中对其进行迭代:
// Create an array with the offsets in it
var offsets = [10, 20, 60, 71];
window.setInterval("showpasttime()", 1000);
function showpasttime() {
// Iterate over the array of times
for (var i = 0; i < offsets.length; i ++) {
d = new Date ();
d.setMinutes(d.getMinutes()-offsets[i]); //gives me current minutes MINUS 10 minutes
month = d.getMonth () + 1;
h = (d.getHours () < 10 ? '0' + d.getHours () : d.getHours ());
m = (d.getMinutes () < 10 ? '0' + d.getMinutes () : d.getMinutes ());
// Use the loop counter to target each row of your table
document.getElementById("row" + (i + 1)).innerHTML = offsets[i] + 'minutes ago it was '
+ d.getDate () + '.'
+ month + '.'
+ d.getFullYear () +
' - '
+ h + ':' + m ;
}
};
//创建一个包含偏移量的数组
var抵销=[10,20,60,71];
setInterval(“showpasttime()”,1000);
函数showpasttime(){
//迭代时间数组
对于(变量i=0;i
这是一个例子。最好的方法是将您的时间推断放在一个数组中,并使用for循环对其进行迭代
最终JS:
var deductions = [10, 20, 60, 71]; //Set your deductions here
window.setInterval(showpasttime(), 1000);
function showpasttime(){
for(i=0;i<deductions.length;i++){ //Iterate through the time deductions in the array
d = new Date ();
d.setMinutes(d.getMinutes()-deductions[i]); //gives me current minutes MINUS specified number of minutes minutes
month = d.getMonth () + 1;
h = (d.getHours () < 10 ? '0' + d.getHours () : d.getHours ());
m = (d.getMinutes () < 10 ? '0' + d.getMinutes () : d.getMinutes ());
document.getElementById("row"+i).innerHTML = deductions[i]+' minutes ago it was '
+ d.getDate () + '.'
+ month + '.'
+ d.getFullYear () +
' - '
+ h + ':' + m ;
}
};
i
从0开始,将递增到3,因为您有4次扣减
让它只是一个确定性函数。这样,它可以在任何时间工作。您的规范可以很容易地更改,代码更易于重用。名称可能应该从showpasttime
更改为类似getMinutesAgo
的内容,以便更具语义
function getMinutesAgo(ago){
d = new Date ();
d.setMinutes(d.getMinutes()-ago);
month = d.getMonth () + 1;
h = (d.getHours () < 10 ? '0' + d.getHours () : d.getHours ());
m = (d.getMinutes () < 10 ? '0' + d.getMinutes () : d.getMinutes ());
return ago + ' minutes ago it was '
+ d.getDate () + '.'
+ month + '.'
+ d.getFullYear () +
' - '
+ h + ':' + m ;
};
函数getMinutesAgo(ago){
d=新日期();
d、 setMinutes(d.getMinutes()-ago);
月=d.getMonth()+1;
h=(d.getHours()<10?'0'+d.getHours():d.getHours());
m=(d.getMinutes()<10?'0'+d.getMinutes():d.getMinutes());
return ago+“几分钟前是”
+d.getDate()+'。'
+月份+'。'
+d.getFullYear()+
' - '
+h+':'+m;
};
然后根据需要消费。除此之外,演示只是我的喜好。给你:这很好用
var分钟=[10,20,60,71];
var newDate=[];
对于(var i=0;i)这样你就可以把你的分钟数放在一个数组中,并使用for循环遍历它们。你只需编辑你的答案,而不是在其中添加注释。答案很好。在演示中,你比较值小于,以确定是否应该添加前导零,你是不是想要10分钟而不是10分钟?太好了,非常感谢。@Jason Aller:我想你是这样的右图:如果您将“h”和“m”变量中的“ago”改回10,脚本将运行得非常好(因为这里的逻辑是:如果分钟/小时小于10,只需在其前面添加一个0,因此它看起来非常棒,如“09:01”,例如:)@freshyeball:我已经尝试使用您的代码(使用jQuery)同样,由于您的代码,我可以更频繁地使用一个“time ago time point”。不幸的是,它对我不起作用。我尝试使用:getMinutesAgo(ago)调用html文档正文中的函数;但不幸的是,什么也没有发生。您能给我一个提示,如何将代码实现到html文档中,或者如何调用函数/代码(我想这完全是一个新手问题,但是对于zsaat14和net.uk.sweet的代码,它通过调用上面提到的函数来工作)?非常感谢!啊,谢谢你,在我尝试了你的链接[link](jsfiddle.net/freshyeball/AUdLb/5)(通过将部分函数放入html文档的头部)之后,它就可以工作了。太好了!@freshyeball最后一个问题/问题:当我在“时间点”之前使用你的代码时,有一个短的时间间隔加载。这意味着:当“时间点”出现(稍晚一点)时,我表中的所有其他内容都已加载。可能是因为jQuery?您能告诉我如何将“/wo jQuery”-代码实现到我的HTML文档中吗?再说一次:在JSFIDLE中,它工作得很好(就像您的演示wo jQuery一样),但当我将确切的代码复制到local.HTML中时(和/或在外部.js文件中),您的代码将无法运行。再次感谢!非常感谢,这非常有效!我刚刚更改了“h”和“m”变量bac中的“扣除[I]”