Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

moment.js is not updating the time, only shows the time when .js file was started

I’m trying to include actual date and time to the certain message, however I’m unable to do it, because moment.js keeps stucked at the same time. For some reason it only triggers the same time over and over again when script was started.

//require moment + time format
var moment = require('moment');
var todaymoment = moment().format('L, hh:mm:ssa');

//Check the current time each 5 seconds and update message
      setInterval(function () {
        message.edit({ embeds: [currentTime.setDescription('Current time is: ' + todaymoment) && currentTime.setTitle('Time Updated!')] })
        console.log(todaymoment)
      }, 5000)
  })

logs:

04/01/2022, 03:30:23am
04/01/2022, 03:30:23am
04/01/2022, 03:30:23am
04/01/2022, 03:30:23am

I also tried different library, but results are same.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This could be because you have initialised the var todayMoment once and you are just printing it again and again every timeout, you are not really getting a new time value every 5 seconds.

You could place your var todaymoment = moment().format('L, hh:mm:ssa'); inside the setTimeout

To explain this you can see the jsFiddle here – https://jsfiddle.net/kjzq567h/4/

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading