ItSolutionStuff.com

JQuery Moment Add Hours to Datetime Example

By Hardik Savani • December 31, 2020
jQuery

Hello all! In this article, we will talk about jquery moment add hours. you will learn moment js add hours to time. We will look at example of moment add hours to current time. Here you will learn moment js add hours to current time. Follow bellow tutorial step of how to add hours to time in jquery moment.

Here, i will give you simple example of jquery moment js add() method for add hours to date and jquery moment js subtract() method for subtract hours from date.

let's see both example with output:

Example 1: Add Hours to Time

<!DOCTYPE html>

<html>

<head>

<title>JQuery Moment Add Hours Example - ItSolutionStuff.com</title>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" crossorigin="anonymous"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" crossorigin="anonymous"></script>

</head>

<body>

<h1>JQuery Moment Add Hours Example - ItSolutionStuff.com</h1>

</body>

<script type="text/javascript">

var startDateTime = '26-12-2020 09:30:20'

var newDateTime = moment(startDateTime, "DD-MM-YYYY hh:mm:ss")

.add(2, 'hours')

.format('DD/MM/YYYY hh:mm:ss');

console.log(newDateTime);

var currentDateTime = moment().add(2, 'hours')

.format('DD/MM/YYYY hh:mm:ss');

console.log(currentDateTime);

</script>

</html>

Output:

26/12/2020 11:30:20

31/12/2020 11:27:14

Example 2: Subtract Hours to Time

<!DOCTYPE html>

<html>

<head>

<title>JQuery Moment Add Hours Example - ItSolutionStuff.com</title>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" crossorigin="anonymous"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" crossorigin="anonymous"></script>

</head>

<body>

<h1>JQuery Moment Add Hours Example - ItSolutionStuff.com</h1>

</body>

<script type="text/javascript">

var startDateTime = '26-12-2020 09:30:20'

var newDateTime = moment(startDateTime, "DD-MM-YYYY hh:mm:ss")

.subtract(2, 'hours')

.format('DD/MM/YYYY hh:mm:ss');

console.log(newDateTime);

var currentDateTime = moment().subtract(2, 'hours')

.format('DD/MM/YYYY hh:mm:ss');

console.log(currentDateTime);

</script>

</html>

Output:

26/12/2020 07:30:20

31/12/2020 07:28:57

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

JQuery Moment Add Minutes to Datetime Example

Read Now →

JQuery Moment Subtract Year from Date Example

Read Now →

JQuery Moment Add Year to Date Example

Read Now →

JQuery Moment Subtract Month Example

Read Now →

JQuery Moment Add Month to Date Example

Read Now →

JQuery Moment Subtract Days to Date Example

Read Now →

JQuery Moment Add Days to Date Example

Read Now →

JQuery Moment Get Current Date Example

Read Now →

How to Install and Use Moment JS in Laravel?

Read Now →

How to Change Date Format using Moment in Vue JS?

Read Now →

How to Change Date Format in JQuery?

Read Now →