【问题标题】:How to get name of the day from date with momentjs如何使用momentjs从日期获取当天的名称
【发布时间】:2026-01-13 01:35:02
【问题描述】:

我正在使用momentjs 并想输出当天的名称,即。 “星期三”,但 API 似乎只提供了一个数字。

有没有办法在不将其硬编码为特定语言的情况下做到这一点?

【问题讨论】:

    标签: javascript momentjs


    【解决方案1】:

    来自Format section of their documentation

    星期几 dddd 星期日 星期一 ... 星期五 星期六

    moment().format('dddd');
    

    【讨论】:

      【解决方案2】:

      使用 moment().format('dddd'); 获取一天的全名,例如 'Sunday' 、'Monday'

      使用 moment().format('ddd'); 获取三个字母的名称,例如 'Sun' 、 'Mon'

      let day_name_full = moment().format('dddd');
      let day_name_three_letter = moment().format('ddd');
      
      console.log('== To Get Day Name ==');
      console.log("using format('dddd') =>",day_name_full);
      console.log("using format('ddd') =>",day_name_three_letter);
      
      
      console.log('== To Get Month Name ==');
      let month_name_full = moment().format('MMMM');
      let month_name_three_letter = moment().format('MMM');
      
      
      console.log("using format('MMMM') =>",month_name_full);
      console.log("using format('MMM') =>",month_name_three_letter);
      <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous"></script>

      【讨论】:

        【解决方案3】:

        如果您想从索引日中获取名称

        const day = 1; //second index after 0
        moment().day(day).format("dddd") //Monday
        

        【讨论】: