0

I want show time under message as 1 min and 2 min etc. Time should be calculated according to the message arrival. Now I'm having this format 4:33:08 AM and I want to calculate it according to message arrival and display it like Examples 1min 4min 25min, I just wanted in that format. Help me finding the solution.

enter image description here

var today = new Date();
var time = today.toLocaleTimeString();
document.getElementById('message').innerHTML = time;
 color: white;
        position: relative;
        float: left;
        background-color: #343a40;
        line-height: 20px;
        border-radius: 5px;
        padding: 10px 25px 10px 25px;
        margin-left: 15px;
        font-size: 15px;
<div id="message"></div>
Arthur Costa
  • 1,451
  • 19
  • 32
LiN
  • 125
  • 1
  • 14
  • 1
    Possible duplicate of [Get difference between 2 dates in JavaScript?](https://stackoverflow.com/questions/3224834/get-difference-between-2-dates-in-javascript) – nurdyguy Mar 07 '19 at 23:08
  • @nurdyguy you've mistaken. I want to show time under message div according to the message arrival. – LiN Mar 07 '19 at 23:13

2 Answers2

0

For this need it's the best to use library like moment.js https://momentjs.com

EladBash
  • 95
  • 6
0

You can do this using javascript only, but you can use momentJS for do it easily.

Here is a example:

var time1 = moment();
var time2 = moment().add(10,'minutes')

time2.diff(time1,'minutes') // 10
time1.diff(time2,'minutes') // -10

You can see more in https://momentjs.com/docs/#/displaying/difference/

Also, if you don't want to add momentJS, a good alternative to moment is: Date FNS.

And after you get the result, you just need to put inside the div.

Arthur Costa
  • 1,451
  • 19
  • 32