0

How to add a second to a date in below format in Javascript?

x = '2015-05-23 11:20:33';

setInterval(function () {
  console.log(x+1);  //Looking something in this place to add a second and show in the same format
}, 1000); 
Malaiselvan
  • 1,153
  • 1
  • 16
  • 42

1 Answers1

1

Use setSeconds()

var date = new Date('2015-05-23 11:20:33');

setInterval(function () {
  date.setSeconds(date.getSeconds() + 1);
  console.log(
    date.getFullYear()+'-'+
    (date.getMonth()+1)+'-'+
    date.getDate()+' '+
    date.getHours()+':'+
    date.getMinutes()+':'+
    date.getSeconds()
    );//to ensure log is  'YYYY-MM-DD HH:MM:SS' format
}, 1000);

See Add 10 seconds to a Javascript date object timeObject

Community
  • 1
  • 1
AmmarCSE
  • 30,079
  • 5
  • 45
  • 53
  • This shows the output in milliseconds. I want the time in the same format as 'YYYY-MM-DD HH:MM:SS' and in the above example it should start exactly from '2015-05-23 11:20:34'. – Malaiselvan May 23 '15 at 11:55