当前日期和当前日期+2天的Javascript脚本日期计算

时间:2020-09-13 19:41:24

标签: node.js reactjs

在React JS中提交表单后,在后端Node JS中,我要求捕获格式为“ 2020年11月26日”的当前日期以及基于当前日期+ 2天(即2020年11月28日)。

使用JavaScript即时获取当前日期
currentDate=moment(new Date(Date.now())).format("MMMM D, YYYY")

将来的日期:currentDate.moment().add(2).format("MMMM D, YYYY")

如果可以的话,请让我使用在Node js中使用的精致代码更好的方法。

1 个答案:

答案 0 :(得分:0)

只需执行以下操作,就可以大大简化将当前日期作为字符串获取的过程:

moment().format("MMMM D, YYYY");

关于添加两天的第二个问题,我将使用以下内容来澄清您要添加的内容:

moment().add({days:2}).format("MMMM D, YYYY");

因此,将两者结合起来就可以做到:

const now = moment();
const nowAsFormattedStr = now.format("MMMM D, YYYY");
// note: use .clone() if you don't want "now" to be altered by adding two days
const inTwoDaysAsFormattedStr = now.clone().add({days:2}).format("MMMM D, YYYY");