admin管理员组文章数量:1415467
How can I get the integer equivalent of days of the week i.e monday
to 2
, tuesday
to 3
, wednesday
to 4
, etc?
Here is what I have:
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
and i need to do
console.log(days['monday']) //i need to see the integer 2
console.log(days['tuesday']) //i need to see the integer 3
How can I get the integer equivalent of days of the week i.e monday
to 2
, tuesday
to 3
, wednesday
to 4
, etc?
Here is what I have:
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
and i need to do
console.log(days['monday']) //i need to see the integer 2
console.log(days['tuesday']) //i need to see the integer 3
Share
Improve this question
edited May 23, 2018 at 8:20
Edwin O.
asked May 13, 2014 at 20:03
Edwin O.Edwin O.
5,30645 silver badges45 bronze badges
1
- Could you provide an example of an input and an example of your desired output? For example: input: bar = new Date("2014 May 13"); foo = dayOfWeekNumber(bar); expected output: foo = 2; Also, please provide a full code snippet of what you've written thus far. As it is, it looks like you're just asking people to write code for you. – Conspicuous Compiler Commented May 13, 2014 at 21:26
3 Answers
Reset to default 4Can 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
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.
Typically, for something like this, you'll likely want to create a function so you don't have to repeat yourself (see DRY). Here's a sample function, that also converts the ining week day string to lowercase so "Wednesday" will return the same result as "wednesday".
Code (demo)
Note: Comment block with function documentation is using style remended by JSDoc.
/**
* @param {String} weekDay
* @returns {Number} Day of week weekDay is. Returns `undefined` for invalid weekDay.
*/
function getWeekDayNumber(weekDay) {
var days = {
sunday: 1,
monday: 2,
tuesday: 3,
wednesday: 4,
thursday: 5,
friday: 6,
saturday: 7
};
return days[weekDay.toLowerCase()];
}
var day = getWeekDayNumber("Sunday"); // `day` now equals `1`
本文标签: javascriptHow to convert day of week name to day of week numberStack Overflow
版权声明:本文标题:javascript - How to convert day of week name to day of week number - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745148288a2644799.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论