How to get the day of the week from the day number in JavaScript?

后端 未结 8 1141
野的像风
野的像风 2020-12-03 07:05

Given dayNumber is from 0 - 6 representing Monday - Sunday respectively.

Can the Date /

8条回答
  •  一个人的身影
    2020-12-03 07:24

    This code is a modified version of what is given above. It returns the string representing the day instead

    /**
    * Converts a day number to a string.
    *
    * @param {Number} dayIndex
    * @return {String} Returns day as string
    */
    function dayOfWeekAsString(dayIndex) {
      return ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"][dayIndex] || '';
    }
    

    For example

    dayOfWeekAsString(0) returns "Monday"
    

提交回复
热议问题