首页 > 解决方案 > 如何仅使用月份名称使用 MomentJS 设置完整日期?

问题描述

我想知道 MomentJS 是否有任何选项来设置完整日期,例如"2019-03-01T00:00:00.000Z"使用月份的字符串名称Mar

我要做的是像这样获取所选月份的交易。但这仅适用于当月;

const month = moment.utc();  // here I would use something to define the selected month
const startMonth = moment(month).startOf("month");
const endMonth = moment(month).endOf("month");
const monthTransaction = getUserTransactions.filter(
  transaction =>
    transaction.createdAt > startMonth.toISOString() &&
    transaction.createdAt < endMonth.toISOString()
);

标签: javascriptmomentjs

解决方案


如果您有月份名称,则可以执行以下操作将其解析为时刻日期:

moment("Mar", "MMM")

有关解析选项的更多信息,请参见此处https://momentjs.com/docs/#/parsing/string/


推荐阅读