javascriptmomentjsmoment-timezone

Get list of abbreviated day of the week in moment using


I am using moment and I want to get list of abbreviated day of the week.

so ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]

I want to be able to use different languages to get these

let resultDates = []; // array to hold day names
const current = moment(); // current date
let n = 7; // days to go back
    while (n > 0) {
      resultDates.push(current.format("dd"))
      current.subtract(1, "day")
      n--;
    }

    console.log(resultDates);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.37/moment-timezone.min.js" integrity="sha512-47dC/gEh5wtWWlul/EJWs2WuGzKoQmn5zn68ZL4gXalKmUhfmH3RkQsU4jTEV9Rmjf204INzBGHbG4xocf2PNw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

I was able to get a list of days but starts with today, how can I arrange it so it always start with Sunday? also I want to be able to use different languages like Spanish


Solution

  • If you import moment-with-locales.js instead of locales you can access the localization data in moment.localeData(locale). Examples:

    console.log(moment.localeData("en")["_weekdaysMin"])
    console.log(moment.localeData("es")["_weekdaysMin"])
    
    console.log(moment.localeData("en")["_weekdaysShort"])
    console.log(moment.localeData("es")["_weekdaysShort"])
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment-with-locales.js"></script>