Skip to content
Advertisement

How to set user-defined time in Javascript and add 2 hours to it?

The user is providing time in the format of 12:00:00 (which is in string), and I want to add 1 or 3 hours to the time and show it as the end time.

The code:

JavaScript

How I am adding the time:

JavaScript

I know I am doing something wrong, but can’t figure out what?

Thanks in advance!

Advertisement

Answer

You have to remember that Date class has some limitation on creating it, so you may not just set your time on the constructor, it must be in a specific format. For more details take a look on the JS Date class documentation.

Your time calculation can be done with a similar code to this:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement