Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Mehul Lakhanpal
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
Enter fullscreen modeExit fullscreen mode

Thanks for reading 💙

Follow@codedrops.tech for more.

InstagramTwitterFacebook

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
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
stegriff profile image
Ste Griffiths
Do justly, love mercy, code humbly. Following Jesus and flipping bits. #BuildCoolStuffTogether
  • Location
    Southport, UK
  • Education
    yes
  • Pronouns
    He/Him
  • Work
    dev @ 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;}
Enter fullscreen modeExit fullscreen mode
CollapseExpand
 
ml318097 profile image
Mehul Lakhanpal
Full-stack Developer | Building Code404.co & Grid | Prev. built QuickSwitch, Array Builder, FileOps
  • Email
  • Location
    Bangalore
  • Work
    Full stack developer
  • Joined

Great i did not know that. Thanks 😀

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Full-stack Developer | Building Code404.co & Grid | Prev. built QuickSwitch, Array Builder, FileOps
  • Location
    Bangalore
  • Work
    Full stack developer
  • Joined

More fromMehul Lakhanpal

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp