Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Richard Haines
Richard Haines

Posted on

     

Date object snippet

Getting the locale date

The toLocaleDateString method on the date object lets you pass in the locale of choice and returns a string formatted date with forward slash separators.

lettoday=newDate().toLocaleDateString('en-gb');// today => 24/09/2020
Enter fullscreen modeExit fullscreen mode

Replacing the slash

To replace the forward slash with a dash is as easy as manipulating the string with a replace regex.

lettoday=newDate().toLocaleDateString('en-gb').replace(/\//g,'-');// today => 24-09-2020
Enter fullscreen modeExit fullscreen mode

Written representation

To get the written representation of the date in the chosen locale toLocaleDateString accepts a second options param where you can specify

  • weekday
  • year
  • month
  • day
constoptions={weekday:'long',year:'numeric',month:'long',day:'numeric',};lettoday=newDate().toLocaleDateString('en-gb',options);// today => Thursday, 24 September 2020
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

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

Dismiss

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

Writing docs @prisma I love to create content and fun experiences while exploring new technologies
  • Location
    Örnsköldsvik
  • Work
    Tech Writer at Prisma
  • Joined

More fromRichard Haines

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