DEV Community

Mahmoud Elmahdi
Mahmoud Elmahdi

Posted on

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

Top comments (0)