-2

there is a test route in nodejs as shown below

router.get('/testdata', function (req, res) {
    var startTime="2016-11-30 23:40:00"
    var endTime = new Date("2016-11-30 23:40:00");
    endTime.setSeconds(endTime.getMinutes() + 2)
    console.log("************getDeviceData*********************")
    console.log(startTime)
    console.log(endTime);
    dataTrackerInfoDetails.timerId = setInterval(function(){
        startTime=endTime;
        var endTime = new Date(startTime);
        endTime= endTime.setSeconds(endTime.getMinutes() + 2)
        console.log("************after first*********************")
        console.log(startTime)
        console.log(endTime);

    },dataTrackerInfoDetails.timerInterval*1000);
})

Every 10 seconds i need to get the date format as shown below with 2 minutes increment from the current date

************getDeviceData*********************
2016-11-30 23:40:00
2016-11-30T18:10:42.000Z
************after before*********************
2016-11-30 23:40:00
undefined

But i will get undefined

expected output is

************getDeviceData*********************
  Start date  2016-11-30 23:40:00
    2016-11-30T18:10:42.000Z
    ************after before*********************
    2016-11-30T18:10:42.000Z
     2016-11-30T18:10:44.000Z
  ************after before*********************
    2016-11-30T18:10:44.000Z
     2016-11-30T18:10:46.000Z.......

Note the date should be same format 2016-11-30T18:10:44.000Z please let me now how to do it

DhanaLaxshmi
  • 424
  • 1
  • 11
  • 24
  • Don't redefine `endTime` remove `var` from `var endTime = new Date(startTime);` – Satpal Nov 28 '17 at 11:11
  • i tried that i will get the time in ms 1480529442000 i wanted the for in t 2016-11-30T18:10:44.000Z – DhanaLaxshmi Nov 28 '17 at 11:15
  • Note that "2016-11-30 23:40:00" is not a valid ISO 8601 date string so is treated as an invalid date by at least one implementation in common use. See [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Nov 29 '17 at 04:34

1 Answers1

0

Your issue is here:

setInterval(function(){
    startTime=endTime;
    var endTime = new Date(startTime);

The declaration of endTime with var creates a local variable with a value of undefined before the function is executed. So when the line:

startTime=endTime;

is executed, startTime is set to undefined. Then:

var endTime = new Date(startTime);

will assign an invalid date to endTime. Also, where you have:

endTime = endTime.setSeconds(endTime.getMinutes() + 2)

you are mixing setSeconds with getMinutes, and the return value from setSeconds is a time value (i.e. number), not a Date, so even if you fix the other errors, you'll get an error as numbers don't have Date methods.

What you probably want to do is:

// Using an IIFE to mimic a callback
(function(){
  // Create start and end as Dates, use valid ISO 8601 format
  var startTime = new Date("2016-11-30T23:40:00");
  var endTime = new Date("2016-11-30T23:40:00");
  // Fix second/minutes mixup
  endTime.setMinutes(endTime.getMinutes() + 2);
  console.log("************getDeviceData*********************")
  console.log(startTime)
  console.log(endTime);
  
  // Just call the timer
  setInterval(function(){
    startTime = endTime;
    // Don't declare a local endTime
    endTime = new Date(startTime);
    // Modify endTime in place and fix seconds/minutes
     endTime.setMinutes(endTime.getMinutes() + 2)
     console.log("************after before*********************")
     console.log(startTime)
     console.log(endTime);
  }, 1000); // run at 1 second intervals for test
}());

You probably want to put some limit on how often this runs. Keep a reference to the value returned by setInterval and then use it to call clearInterval when some condition is met.

RobG
  • 142,382
  • 31
  • 172
  • 209