It has to be passed on subtract
a corresponding duration which 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>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
It can also be passed by a direct setting on subtract
one {h:3}
which is the amount of hours, example:
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>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
this being very direct to what needs to subtract.
Another way is also using the two parameters 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(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>
<script src="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>
<script src="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 being 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 on:
You have the result of value 02/08/2018 13:37 and want the result to be 02/08/2018 10:37, you want to take 3 hours from the first date?
– novic
@Virgilionovic yes.
– Matheus Miranda