DEV Community

Mahmoud Elmahdi
Mahmoud Elmahdi

Posted on

4

JavaScript Date methods guide

getDate method

The getDate() method returns the day of the month (from 1 to 31) for the given date according to local time.

Usage

/*
 * Representing the day of the month
 * @return {Number} - An integer number, between 1 and 31
 */

const date = new Date()
date.getDate();
Enter fullscreen mode Exit fullscreen mode

Browser compatibility: Chrome, Firefox (Gecko), IE, Opera, Safari

getDay method

The getDay() method returns the day of the week (from 0 to 6) for the specified date according to local time, where (Sunday is 0, Saturday is 6).

Usage

/*
 * Representing the day of the week
 * @return {Number} - An integer between 0 and 6
 */

const date = new Date()
const weekDays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];

weekDays[date.getDay()]; // String
Enter fullscreen mode Exit fullscreen mode

Browser compatibility: Chrome, Firefox (Gecko), IE, Opera, Safari

toDateString method

The toDateString() method returns the date (not the time) portion of a Date object into a human readable form.

Usage

/*
 * Representing the Date object in human readable
 * @return {String} - Date portion of the given Date object
 */

const date = new Date()
date.toDateString();
Enter fullscreen mode Exit fullscreen mode

Browser compatibility: Chrome, Firefox (Gecko), IE, Opera, Safari

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay