Momentjs - How to add/subtract datetime?

Asked

Viewed 3,993 times

1

Follows code:

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 with the following code and result is the same of the variable datetime_utc:

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

See documentation: https://momentjs.com/docs/#/manipulating/utc-offset/

In the variable utc, he may have a negative or positive number.

  • 1

    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?

  • 1

    @Virgilionovic yes.

2 answers

3


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:

0

What result do you want for sure?

You got the 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 function utc()

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

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

var offset = moment().utcOffset();
var convertDatetime = moment(datetime).utcOffset(offset);
  • I tried the function zone() the result is the same.

  • The result I want is: 02/08/2018 10:37.

  • According to the documentation: This Function has been deprecated in 2.9.0. Consider Moment.fn.utcOffset Instead.

  • Do not use the function zone()

  • Look at this one: http://jsfiddle.net/9PAFg/2799/

  • Date and month reversed.

  • It seems to me that it is to convert "02/08/2018 13:37 " to timestamp, to make it work.

  • Sometimes this confusion can happen with the day/month they change, depending on what the Moment() understands of the string you pass to it, but you can specify which input format passing the format it is in as 2nd parameter. var convertDatetime = moment('02/08/2018 15:36', 'DD/MM/YYYY HH:mm').utcOffset(offset).format("DD/MM/YYYY HH:mm");

  • Let’s clear comment ?

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.