簡體   English   中英

如何獲得上個月的第 15 天和本月的第 16 天

[英]How to get last month day 15 and current month day 16 in moment

我正在嘗試獲得第 15 天的最后一個月和第 16 天的當前月份,但我失敗了,有人可以幫助我如何解決這個問題。

預期結果 => 2020 年 11 月 15 日 10 月 15 日

要獲得上個月的日期 15,您需要:

moment().subtract(1, 'month').date(15);

您減去一個月並將日期設置為 15。這將返回 10 月 15 日。

要獲得當前日期 15,只需刪除減法部分。

要獲得您當時要求的准確結果:

const currentMonthDate15 = moment().date(15);
const lastMonthDate15 = moment().date(15).subtract(1, 'month');
const string = lastMonthDate15.format('DD MMM') + ', ' + currentMonthDate15.format('DD MMM YYYY');

其中 string 是15 Oct, 15 Nov 2020

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM