【问题标题】:Getting the month number by month name with Moment.js使用 Moment.js 按月份名称获取月份编号
【发布时间】:2015-12-11 10:59:14
【问题描述】:

我正在尝试使用 MomentJS 返回通过月份名称的月份编号。例如,如果我将“July”传递给 moment(),我希望返回 7。

在阅读完文档后,我尝试了几种不同的方法,并且这种方法很接近......

console.log(moment().month("July"));

在控制台中,隐藏在响应中我可以看到这个......

_monthsParse: Array[7]

谁能告诉我如何正确使用 MomentJS 返回月份数?

【问题讨论】:

    标签: momentjs


    【解决方案1】:

    试试看:

    moment().month("July").format("M");
    

    相关文档:http://momentjs.com/docs/#/get-set/month/

    alert(moment().month("July").format("M"));
    <script src="https://momentjs.com/downloads/moment.min.js"></script>

    【讨论】:

      【解决方案2】:

      任何想从月份编号中获取月份名称的人都可以尝试:

      const number = 1; // 0 = Jan & 11 = Dec
      moment().month(number).format("MMM"); // Feb
      

      使用以下获取完整的月份名称:

      const number = 1; // 0 = January & 11 = December
      moment().month(number).format("MMMM"); // February
      

      【讨论】:

      • 加一个.. 有趣的是,这就是我正在寻找的东西,我只是检查了这个问题以获得一个想法。
      【解决方案3】:
      ##get month name in moment js with node js 
      moment() give today date
      format("DD-MMMM-YYYY") / output 18-May-2020
      format("DD-MM-YYYY")  / output 18-05-2020
      - sperator you can use / 
      
      ```
      var moment = require('moment'); 
      
         m_date = moment().format("DD-MMMM-YYYY");
         console.log("moment date :", m_date)
      ```
      ##output
      ```
      moment date : 18-May-2020
      
      ```
      

      Read Officail Docdescription here

      【讨论】:

        猜你喜欢
        • 2017-10-03
        • 2011-03-12
        • 1970-01-01
        • 2017-02-19
        • 2015-09-25
        • 2015-11-29
        • 2017-08-15
        相关资源
        最近更新 更多