Skip to content
Advertisement

How do i subtract time from a datetime-local input?

Hi I’m a newbie in JavaScript and have little to no experience in it. Clicking the button will print the input time.

What i was trying to do was subtract the time by 9 hours 30 minutes and I’m unable to find a way to do it.

It will help me a lot if someone could explain how to do this.

I’ve attached the whole HTML document below.

JavaScript
JavaScript

Advertisement

Answer

You have to make use of the Date Object.

  1. Pass the input as a Date object
  2. Set the new date by subtracting hours and minutes as necessary

Side Note: If you have more complex operations which include dates, you can make use of a third-party library like Moments Js

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