How to add hours in datetime using JavaScript

edited July 2023 in Web Development

In this article we are seeing the how to add hours in datetime using Javascript

HTML:

 <input type='text' class="form-control date"  name="datetime"  id='datetimepicker4'   value="" placeholder="Datetime"/>

By using ID call the Value from HTML by using jQuery.

  var date = $('#datetimepicker4').val();

After date value need to convert to the Date Object in JavaScript.

  var datetime = new Date(date);      

Need to add hours to the datetime     

 code:

 datetime.setHours(datetime.getHours()+2)      

After that need to append the value to input tag in html.        

    $('#datetimepicker3').val(datetime.toLocaleString())

Code:


Tagged:
Sign In or Register to comment.