【问题标题】:How to get the start date and end date of a month by passing Month and Year in Moment.js如何通过在 Moment.js 中传递月份和年份来获取一个月的开始日期和结束日期
【发布时间】:2023-03-10 01:27:01
【问题描述】:

我正在使用 Moment.js 进行日历转换。是否可以通过传递月份和年份来获取月份的第一个和最后一个日期。

我的月份和年份格式是 - 10-18,它是 MM-YY 格式。

我想获取 10 月的第一个和最后一个日期,例如 01 Oct 2018。 我可以在 Moment 中将其格式化为我想要的日期,但不知道如何通过 10-18 获取一个月的第一个和最后一个日期。

任何帮助建议都会非常有帮助。

谢谢 回复

【问题讨论】:

标签: javascript react-native momentjs date-formatting


【解决方案1】:

很简单,只需使用startOfendOf ;)

小心,因为它会改变原始时刻。

const input = "10-18";
const output = moment(input, "MM-YY");
console.log('Start of the month:', output.startOf('month').format('LL'));
console.log('End of the month:', output.endOf('month').format('LL'));
<script src="https://momentjs.com/downloads/moment.js"></script>

这是文档:

【讨论】:

  • 不要忘记将答案标记为已接受,以便它可以为社区的其他人服务;)
  • 请不要回答重复的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-29
  • 1970-01-01
  • 2021-12-16
相关资源
最近更新 更多