简体   繁体   English

使用Moment.js返回值

[英]Returning a value using Moment.js

I'm using some of the date manipulation functions of moment.js such as add days. 我正在使用moment.js一些日期操作功能,例如添加天数。

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? 如何返回简单的日期值?

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.. 若要获取Moment.js包装的本机Date对象的副本,请使用.toDate()函数。

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

To get a copy as an ISO formatted string.. 以ISO格式的字符串获取副本。

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 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 return moment(date).add(days, 'd').toString(); // Sat Apr 30 2016 16:59:46 GMT-0500return moment(date).add(days, 'd').format(); // 2013-03-10T01:30:00-05:00 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.com/docs/#/get-set/ 查看文档以获取更多信息: https : //momentjs.com/docs/#/get-set/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM