admin管理员组

文章数量:1397118

I'm using some of the date manipulation functions of moment.js such as add days.

import moment from 'moment';

const addDays = (date, days) => {
    return moment(date).add(days, 'd');
}

This seems to work but returns an object. How do I return a simple date value?

I'm using some of the date manipulation functions of moment.js such as add days.

import moment from 'moment';

const addDays = (date, days) => {
    return moment(date).add(days, 'd');
}

This seems to work but returns an object. How do I return a simple date value?

Share Improve this question asked Jun 28, 2017 at 22:50 SamSam 30.6k76 gold badges252 silver badges464 bronze badges 1
  • just return moment(date).add(days, 'd').format('YYYY-MM-DD'); – usrNotFound Commented Jun 28, 2017 at 22:52
Add a ment  | 

1 Answer 1

Reset to default 4

Depends on how you want it formatted and what data type.


To get a copy of the native Date object that Moment.js wraps, use the .toDate() function..

return moment(date).add(days, 'd').toDate();

To get a copy as an ISO formatted string..

return moment(date).add(days, 'd').toISOString();

To get a copy as a true string (what I think you want)..

return moment(date).add(days, 'd').toString(); // Sat Apr 30 2016 16:59:46 GMT-0500 or return moment(date).add(days, 'd').format(); // 2013-03-10T01:30:00-05:00

To get a copy as a string in a certain format..

return moment(date).add(days, 'd').format('MMMM Do YYYY, h:mm:ss a'); // June 28th 2017, 3:55:57 pm


Check out the docs for more info: https://momentjs./docs/#/get-set/

本文标签: javascriptReturning a value using MomentjsStack Overflow