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
1 Answer
Reset to default 4Depends 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
版权声明:本文标题:javascript - Returning a value using Moment.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744139744a2592565.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论