0

[code][1]

Hi I am trying to find the difference between these 2 dates and would like the answer to be set like this - 00:00:00. I have "current" set to a

as 00:00:00 but would like the time stamp to be updated to however many hours, min, seconds there are between the 2 days. I'm being told that the getHours, getMinutes, and getSeconds function is not a thing. How do I go about fixing this? [1]: https://i.stack.imgur.com/1GY5B.png

unknown
  • 1
  • 2
  • Please read [how to ask](https://stackoverflow.com/help/how-to-ask) and edit your question with the relevant code. Note that this means adding the actual code to the question, not just an image of your code. Additionally, a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) will make this easier to answer. Have you read the documentation about `Date`? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date What specifically isn't working? – WOUNDEDStevenJones Mar 08 '21 at 21:27
  • Please add the actual source code in the question. Images of code are not very useful to re-create and answer the question – maazadeeb Mar 08 '21 at 21:27
  • 1
    Welcome to SO, please see [ask]. Especially the part where it says "DO NOT post images of code, data, error messages, etc. - copy or type the text into the question.". – Roope Mar 08 '21 at 21:28
  • Does this answer your question? [How to calculate date difference in JavaScript?](https://stackoverflow.com/questions/7763327/how-to-calculate-date-difference-in-javascript) – WOUNDEDStevenJones Mar 08 '21 at 21:31

1 Answers1

0

I guess you didnt convert the time to a Date.

look at my example:

function updateTime(){
var event = new Date("April 1, 2021 8:30:00").getTime();
var now = new Date().getTime();
var a = (event - now);
var d = new Date(a);

var hours = d.getHours(),
    minutes = d.getMinutes(),
    seconds = d.getSeconds(),
    ampm = "AM";
    
    if (hours>12) ampm = "PM";
    
    console.log(`${hours}:${minutes}:${seconds} ${ampm}`)
}


updateTime();

Is this what you try to do?

PS: However I would also multiply the hours with the days or I would also add days if days > 0

And the "AM" is kinda unnecessary for getting a difference

MrJami
  • 685
  • 4
  • 16
  • Using the built–in parser for unsupported timestamp formats is strongly discouraged, see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) There are also many posts on [getting the difference between two dates](https://stackoverflow.com/search?q=%5Bjavascript%5D+difference+between+dates). – RobG Mar 08 '21 at 23:32
  • @RobG I agree, tried just to solve his issue in this particular case. I personally use momentJS for these kind of stuff – MrJami Mar 09 '21 at 09:00