简体   繁体   中英

Convert date from Day of Week to Day Name

I'm using moment.js to manipulate my dates. I retrieve the data in which the field day is represented as numeral, like 1 for Monday, 2 for Tuesday and so on. How can I convert those int back to "Monday, Tuesday" etc using moment.js?

Use day(arg) method to retrieve the data object and then use format("dddd") to retrieve the day of week string.

 var date = moment(); let dayNumber = 4; let dayString = date.day(dayNumber).format("dddd"); console.log(dayString);
 <script type="text/javascript" src="https://rawgit.com/moment/moment/2.2.1/min/moment.min.js"></script>

You can have an array of strings with the days of the week in the order that you want:

    const daysOfWeek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

    function getDayOfWeek(number){
        return daysOfWeek[number];
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM