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

前端 未结 3 1761
一生所求
一生所求 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:42

    Can use a lookup object:

    var days = {
        sunday: 1,
        monday: 2,
        tuesday: 3,
        wednesday: 4,
        thursday: 5,
        friday: 6,
        saturday: 7
    }
    
    console.log(days["sunday"]) //1
    

    Now, simply access any of the above properties and it's value will be the numeric day of the week!

    console.log(days["monday"]) //2
    

提交回复
热议问题