使用javascript向datetime添加第二个

时间:2015-05-23 11:48:15

标签: javascript datetime

如何在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); 

1 个答案:

答案 0 :(得分:1)

使用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);




请参阅Add 10 seconds to a Javascript date object timeObject