How to convert day of week name to day of week number

前端 未结 3 1737
一生所求
一生所求 2021-01-23 00:03

How can I get the integer equivalent of days of the week i.e monday to 2, tuesday to 3, wednesday to 4

3条回答
  •  星月不相逢
    2021-01-23 00:32

    You could also use the "indexOf" function of javascript.

    var days  = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
    
    console.log(days.indexOf('Sunday')); // return 0
    console.log(days.indexOf('Tuesday')); // return 2
    

    But remember that "indexOf" is case sensitive and that it will return "-1", if the string is not in the array.

提交回复
热议问题