【问题标题】:How to show dynamic dates with month name of current month and previous month in momentjs?如何在momentjs中显示带有当前月份和上个月月份名称的动态日期?
【发布时间】:2021-07-29 13:48:01
【问题描述】:

如何在 moment.js 中获取当前和上个月的所有日期? 我能够得到月份,但无法找到月份的日期 如果获取月份,下面的代码是否也有任何类似的获取日期的代码?

var labels = moment.monthsShort();

我想展示类似的东西 this image

【问题讨论】:

    标签: javascript jquery charts chart.js momentjs


    【解决方案1】:

    //sample to display february days
    var months = moment.monthsShort();
    
    var datesofmonth = createAllDatesOfMonth(months[1], 2021)
    
    console.log(datesofmonth);
    
    
    function createAllDatesOfMonth(month, year) {
      let year_month = year + "-" + month;
      let nbdays = moment(year_month, "YYYY-MMM").daysInMonth();
      return Array.from(Array(nbdays).keys()).map((d) => month + " " + (d + 1));
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.5.1/moment.min.js"></script>

    【讨论】:

    • 非常感谢,先生。它真的帮助了我。
    • @Divyesh 很高兴为您提供帮助,不要忘记验证并投票结束问题
    • 已结束的问题。仍然需要更多的声誉来投票。一定会尽可能做到。
    猜你喜欢
    • 2020-11-29
    • 2020-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-23
    • 1970-01-01
    • 2019-11-09
    相关资源
    最近更新 更多