DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Rupak Dey
Rupak Dey

Posted on

Ways to get a DATE πŸ˜‰

...in JAVASCRIPT πŸ˜‚

Save for later.


1. Date.now()

Date.now()
Enter fullscreen mode Exit fullscreen mode

This method returns the number of milliseconds elapsed since Jan 1, 1970 00:00:00 UTC.


2. getFullYear()

new Date().getFullYear()
Enter fullscreen mode Exit fullscreen mode

Get the year as a 4 digit number (yyyy).


3. getMonth()

new Date().getMonth() + 1
Enter fullscreen mode Exit fullscreen mode

Get month as a number (0-11).


4. getDay()

const day = new Date().getDay()
const days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thurs', 'Fri', 'Sat']
console.log(days[day])
Enter fullscreen mode Exit fullscreen mode

Get weekday as a number (0-6).


5. getDate() πŸ’•πŸ˜Ž

new Date().getDate()
Enter fullscreen mode Exit fullscreen mode

Get the day as a number (1-31).


Hope you find one soon! 😁


P.S. Want the next post to be something specific? Do let me know in the comments.

🀘🏻

Top comments (1)

Collapse
barelyhuman profile image
Reaper

people looking to handle simple date formatting should checkout github.com/lukeed/tinydate by Luke , found it a week ago

πŸ€” Did you know?

Β 
πŸ—Ί We put together some guides that include some of our favorite content on DEV. Check them out here.