Mehul Lakhanpal
Posted on • Originally published atcodedrops.tech
Custom util to get weekday name or number
constgetDayNameOrNo=(value)=>{constmappings=[[0,"sunday"],[1,"monday"],[2,"tuesday"],[3,"wednesday"],[4,"thursday"],[5,"friday"],[6,"saturday"],];constmatch=mappings.find(([dayNo,name])=>dayNo===value||name===value);if(!match)return;const[matchedNo,matchedName]=match;returnmatchedNo===value?matchedName:matchedNo;};console.log(getDayNameOrNo(0));// sundayconsole.log(getDayNameOrNo("sunday"));// 0console.log(getDayNameOrNo("friday"));// 5
Thanks for reading 💙
Follow@codedrops.tech for more.
Instagram ●Twitter ●Facebook
Micro-Learning ● Web Development ● Javascript ● MERN stack
codedrops.tech
Projects
File Ops - A VS Code extension to easily tag/alias files & quick switch between files
Top comments(2)
Subscribe

Ste Griffiths•
Do justly, love mercy, code humbly. Following Jesus and flipping bits. #BuildCoolStuffTogether
- LocationSouthport, UK
- Educationyes
- PronounsHe/Him
- Workdev @ paymentshield/atlanta
- Joined
Thanks for the post. I think you could improve it by removing the hardcoded English day names and using the JS built-in features to get the day of the week in yourlocale
.
Typescript:
getWeekDays(locale):string[]{varbaseDate=newDate(Date.UTC(2017,0,1));// just a SundayvarweekDays=[];for(leti=0;i<7;i++){letdayName=baseDate.toLocaleDateString(locale,{weekday:'long'});dayName=dayName[0].toUpperCase()+dayName.slice(1);weekDays.push(dayName);baseDate.setDate(baseDate.getDate()+1);}returnweekDays;}

Mehul Lakhanpal•
Full-stack Developer | Building Code404.co & Grid | Prev. built QuickSwitch, Array Builder, FileOps
- Email
- LocationBangalore
- WorkFull stack developer
- Joined
Great i did not know that. Thanks 😀
For further actions, you may consider blocking this person and/orreporting abuse