How to manipulate current time in Javascript (several times and add it to the table)?

What I want to do:

I want to change the current time again and display them in different rows of the table in my HTML document.

Example:

I want to get the time that was 10, 20, 60 and 71 minutes ago from the current time. So if the current time on my computer is 23:00 (in Europe) now, it should give me 22:50, 22:40, 22:00 and 21:49.

What i have done so far:

I managed to change the time once, but I'm not sure how to change it several times (perhaps using the "for" or "at that time" -Loop?) And add them all to the table in my html-document.

This is my .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 ;
  };

Result:

In my html document "10 minutes ago it was 5.3.2014 - 22:50" is included in tablerow with id = "row2".

Problem / Question:

(, , 20, 60 71 ). , . "" ""? ( , , html/css/javascript/jquery).

+4
4

DEMO (/w jQuery)

DEMO (/wo jQuery)

. , . , . , , getMinutesAgo from showpasttime, .

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

. , - .

+3

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

HTML:

JS "row"+i, HTML-:

<div id='row0'></div>
<div id='row1'></div>
<div id='row2'></div>
<div id='row3'></div>

i 0 3, 4 .

+1

array 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 ;
    }
};
0

:

http://jsfiddle.net/zjjbn/1/

var minutes=[10,20,60,71];
var newDate = [];

for(var i=0; i <minutes.length;i++){
   var now = new Date();   //your date
   newDate[i] = new Date(now.getTime() + minutes[i]*60*1000);
   alert(newDate[i]);
}  // now you have a new array  newDate[]  with your new dates
0

Source: https://habr.com/ru/post/1530325/


All Articles