【问题标题】:Isuue for get all months from a year and get all weeks from a month using Moment js使用 Moment js 从一年中获取所有月份并从一个月中获取所有周的问题
【发布时间】:2015-03-27 14:18:52
【问题描述】:

moment.js 是个新手

我想要 3 种类型的输出

  • 获取一年中的所有月份

  • 在 Moment js 中获取一个月的所有周数

  • 获取一周的所有天数

如何使用moment.js 做到这一点?

我已经尝试了整个月

moment().months(2011);// it's working for my year is 2011

但我只有年份的最后 2 位数字。

moment().months(11);// It's give wrong values or my year is 11

我也看过文件,他们说

moment().months()接受从0到11的数字。如果超出范围,它将冒泡到年份。

当我使用 get days 和 week 时也会出现这个问题

如何在 moment.js 中解决这个问题?

【问题讨论】:

    标签: javascript angularjs momentjs


    【解决方案1】:

    使用日期作为参数 (12-12-2011) 查找年月、月周和工作日数值,如果您需要单词(星期一、十二月)值,只需使用 moment.js 的格式和翻译。

    您对 moment().months(2011) 所做的操作有点不正确 - moment() 现在返回日期时间,months(value) 将值月添加到您的时刻检查并查看:

    moment().months(2011).format("LLLL"); //result Tuesday, August 27, 2182 ...
    

    现在阅读一下关于上周 here (52/53) 之间的变化。

    现在解决你的问题,

    一年中的所有月份,老兄,无论如何(12个月):

     moment("12-26-2011", "MM-DD-YYYY").month() + 1; 
    

    获取一年中的周而不是月的周(您会混淆使用月中的周)

     moment("12-26-2011", "MM-DD-YYYY").week();
    

    或试试这个(月周):

    var curr_month = moment("12-26-2011", "MM-DD-YYYY").month(); 
    var prev_month_last_week = moment("01-01-2011", "MM-DD-YYYY").add(curr_month -1, "month").week();
    var your_week_per_month = moment("12-26-2011", "MM-DD-YYYY").week() - prev_month_last_week; //from 1 to 4:
    

    星期几:

    moment("12-26-2011", "MM-DD-YYYY").day();
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2019-07-27
      • 1970-01-01
      • 2015-12-15
      • 2015-10-22
      • 1970-01-01
      • 1970-01-01
      • 2022-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多