Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

JS Date Get Methods

The new Date() Constructor

new Date() generates a date object representing the current date and time.

Get the Current Time

const date = new Date();

Date Get Methods

Method

Description

getFullYear()

Retrieve the year in four-digit format (yyyy).

getMonth()

Retrieve the month as a number, ranging from 0 to 11.

getDate()

Retrieve the day as a number, ranging from 1 to 31.

getDay()

Retrieve the weekday as a number, ranging from 0 to 6.

getHours()

Retrieve the hour in the range from 0 to 23.

getMinutes()

Retrieve the minute within the range of 0 to 59.

getSeconds()

Retrieve the second within the range of 0 to 59.

getMilliseconds()

Retrieve the millisecond within the range of 0 to 999.

getTime()

Retrieve the time expressed in milliseconds since January 1, 1970.

Note 1

The aforementioned get methods return local time. 

Information about Universal Time (UTC) is provided at the bottom of this page.

Note 2

The get methods extract information from existing date objects.

In a date object, the time remains static; it doesn’t change dynamically.

The time within a date object does NOT represent the current time.

The getFullYear() Method

The ‘getFullYear() method retrieves the year of a date as a four-digit number.

Example

const d = new Date(“2021-03-25”);
d.getFullYear();
const d = new Date();
d.getFullYear();

Warning!

Older JavaScript code may utilize the non-standard method ‘getYear(), which is intended to return a two-digit year. However, it’s important to note that ‘getYear() is deprecated and should not be used.

The getMonth() Method

The ‘getMonth() method retrieves the month of a date as a number, ranging from 0 to 11.

Note

In JavaScript, January is represented as month number 0, February as 1, and so forth, with December being month number 11.

Example

const d = new Date(“2021-03-25”);
d.getMonth();
const d = new Date();
d.getMonth();

Note

 

To retrieve the month name, you can utilize an array containing the names of the months.

Example

const months = [“January”“February”“March”“April”“May”“June”“July”,

 “August”“September”“October”“November”“December”];

const d = new Date(“2021-03-25”);
let month = months[d.getMonth()];

const months = [“January”“February”“March”“April”“May”“June”“July”

“August”“September”“October”“November”“December”];

const d = new Date();
let month = months[d.getMonth()];

The getDate() Method

The getDate() function provides the numerical representation of the day within a date (ranging from 1 to 31).

Example

const d = new Date(“2021-03-25”);
d.getDate();
const d = new Date();
d.getDate();

The getHours() Method

The getHours() function yields the numeric representation of the hour within a date (ranging from 0 to 23).

Example

const d = new Date(“2021-03-25”);
d.getHours();
const d = new Date();
d.getHours();

The getMinutes() Method

The getMinutes() function retrieves the numerical representation of the minutes within a date (ranging from 0 to 59).

Example

const d = new Date(“2021-03-25”);
d.getMinutes();
const d = new Date();
d.getMinutes();

The getSeconds() Method

The getSeconds() function provides the numeric representation of the seconds within a date (ranging from 0 to 59).

Examples

const d = new Date(“2021-03-25”);
d.getSeconds();
const d = new Date();
d.getSeconds();

The getMilliseconds() Method

The getMilliseconds() function yields the numerical representation of the milliseconds within a date (ranging from 0 to 999).

Examples

const d = new Date(“2021-03-25”);
d.getMilliseconds();
const d = new Date();
d.getMilliseconds();

The getDay() Method

The getDay() function retrieves the numerical representation of the weekday within a date (ranging from 0 to 6).

Note

In JavaScript, Sunday is considered the first day of the week (day 0). 

However, in some countries, Monday is regarded as the beginning of the week.

Examples

const d = new Date(“2021-03-25”);
d.getDay();
const d = new Date();
d.getDay();

Note

You can utilize an array containing names and utilize getDay() to retrieve the weekday as a name.

Examples

const days = [“Sunday”“Monday”“Tuesday”“Wednesday”“Thursday”

“Friday”“Saturday”];

const d = new Date(“2021-03-25”);
let day = days[d.getDay()];

const days = [“Sunday”“Monday”“Tuesday”“Wednesday”“Thursday”“Friday”,

 “Saturday”];

const d = new Date();
let day = days[d.getDay()];

The getTime() Method

The getTime() function provides the count of milliseconds elapsed since January 1, 1970.

Examples

const d = new Date(“1970-01-01”);
d.getTime();
const d = new Date(“2021-03-25”);
d.getTime();
const d = new Date();
d.getTime();

The Date.now() Method

Date.now() yields the count of milliseconds elapsed since January 1, 1970.

Examples

let ms = Date.now();

Determine the elapsed years since 1970/01/01.

const minute = 1000 * 60;
const hour = minute * 60;
const day = hour * 24;
const year = day * 365;

let years = Math.round(Date.now() / year);

Date.now() is a static method belonging to the Date object.

It cannot be utilized on a date object in the manner of myDate.now().

Its syntax always remains as Date.now().

UTC Date Get Methods

Method

Same As

Description

getUTCDate()

getDate()

Returns the UTC date

getUTCFullYear()

getFullYear()

Returns the UTC year

getUTCMonth()

getMonth()

Returns the UTC month

getUTCDay()

getDay()

Returns the UTC day

getUTCHours()

getHours()

Returns the UTC hour

getUTCMinutes()

getMinutes()

Returns the UTC minutes

getUTCSeconds()

getSeconds()

Returns the UTC seconds

getUTCMilliseconds()

getMilliseconds()

Returns the UTC milliseconds

UTC methods operate using Coordinated Universal Time (UTC), which is equivalent to GMT (Greenwich Mean Time).

The variance between Local time and UTC time can span up to 24 hours.

The getTimezoneOffset() Method

The getTimezoneOffset() function provides the variance (in minutes) between local time and UTC time.

Example

let diff = d.getTimezoneOffset();