Momentjs - How to add / subtract datetime?

1

Follow the code below:

var value = '/Date(1533227866063)/';
var utc = moment().utcOffset() / 60; // -180 / 60 = -3
var datetime_utc = moment(value).format("DD/MM/YYYY HH:mm"); // 02/08/2018 13:37
var convertDatetime = ??? // 02/08/2018 10:37

I tried the following code and the result is the same as the datetime_utc variable:

var convertDatetime = moment(datetime).utcOffset(utc);

See documentation: link

In variable utc , it can be negative or positive number.

    
asked by anonymous 03.08.2018 / 15:03

2 answers

2

It has to be passed in subtract a corresponding duration that in your case is 03:00:00 (3 hours) with the command moment.duration , example :

var value = '/Date(1533227866063)/';

var datetime0 = moment(value)
console.log(datetime0.format("DD/MM/YYYY HH:mm:SS"));    

var time = moment.duration("03:00:00");
var datetime1 = moment(value);
datetime1.subtract(time);
console.log(datetime1.format("DD/MM/YYYY HH:mm:SS"));    
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

It can also be passed through a direct configuration in subtract a {h:3} which is the amount of hours, eg:

var value = '/Date(1533227866063)/';

var datetime0 = moment(value)
console.log(datetime0.format("DD/MM/YYYY HH:mm:SS"));    

var datetime1 = moment(value);
datetime1.subtract({h:3});
console.log(datetime1.format("DD/MM/YYYY HH:mm:SS"));  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

This is very straightforward to what you need to subtract.

Another way is also using the two example parameters:

var value = '/Date(1533227866063)/';

var datetime0 = moment(value)
console.log(datetime0.format("DD/MM/YYYY HH:mm:SS"));    

var time = moment.duration("03:00:00");
var datetime1 = moment(value);
datetime1.subtract(3, 'hours');
console.log(datetime1.format("DD/MM/YYYY HH:mm:SS"));  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

To add only changes the method but, the form is the same example:

var value = '/Date(1533227866063)/';

var datetime0 = moment(value)
console.log(datetime0.format("DD/MM/YYYY HH:mm:SS"));    

var time = moment.duration("03:00:00");
var datetime1 = moment(value);
datetime1.add(3, 'hours');
console.log(datetime1.format("DD/MM/YYYY HH:mm:SS"));  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

These are the possible ways to add / subtract with momentjs .

Your documentation has all this explained

moment().add(Number, String);
moment().add(Duration);
moment().add(Object);
moment().subtract(Number, String);
moment().subtract(Duration);
moment().subtract(Object);

The explanation of the parameters is as follows:

|---------------|-------------|
| Key           | Shorthand   |
|---------------|-------------|
| years         | y           |
| quarters      | Q           |
| months        | M           |
| weeks         | w           |
| days          | d           |
| hours         | h           |
| minutes       | m           |   
| seconds       | s           |
| milliseconds  | ms          |
|---------------|-------------|

Examples

moment().add(7, 'days').add(1, 'months'); 
moment().add({days:7,months:1});

Examples and documentation in:

03.08.2018 / 16:30
0

What result do you intend for?

You have add and the subtract , which you can use to add or remove seconds, minutes, hours, etc.

var convertDatetime = moment(datetime).add(1, 'days');

If you want to convert to UTC, you can use the utc () function

var convertDatetime = moment.parseZone('2016-05-03T22:15:01+02:00').utc().format(); //"2016-05-03T20:15:01Z"

You can use the utcOffset() function over a generated moment using your timestamp to specify the offset.

var offset = moment().utcOffset();
var convertDatetime = moment(datetime).utcOffset(offset);
    
03.08.2018 / 15:21