JavaScript Date Methods

JavaScript Date Methods

Create Date Methods In JavaScript

In JavaScript, the date object is based on a timestamp of milliseconds elapsed since the epoch.

The date output will use the time zone of the local device which is normally the web browser. The date time string is simplified from the ISO 8601 calendar date extended format. The format is YYYY-MM-DDTHH:mm:ss.sssZ. Date methods return specific parts of a date object.

A date object is created via the date constructor and built-in functions can manipulate the object output. The ISO 8601 is maintained by the International Organization for Standardization was published in 1988 and last updated in 2022.

Common Syntax Of Date Methods In JavaScript

Glossary:

Epoch

Time representation of zero.

Unix Epoch

Number of seconds since 1970-01-01 00:00:00 UTC.

Common Date Methods

Ojamboshop.com Learning JavaScript Date Methods
Name Description Example
getFullYear() Returns year as four digit YYYY. date.getFullYear();
setFullYear() Set year as four digit YYYY with optional month MM and day DD. date.setFullYear(2025);
getMonth() Returns month digit(s). date.getMonth();
setMonth() Set month digit(s). date.setMonth(0);
getDate() Returns day digit(s). date.getDate();
setDate() Set day digit(s). date.setDate(10);
getHours() Returns hour digit(s). date.getHours();
setHours() Set hour digit(s). date.setHours(11);
getMinutes() Returns minute digit(s). date.getMinutes();
setMinutes() Set minute digit(s). date.setMinutes(33);
getSeconds() Returns seconds digit(s). date.getSeconds();
setSeconds() Set second digit(s). date.setSeconds(45);
getTime() Returns time in milliseconds since 1970-01-01. date.getTime();
setTime() Set time in milliseconds since 1970-01-01. date.setTime(1736470339876);
Name Description Example

JavaScript Date Methods Snippet

// OjamboShop.com Learning JavaScript Date Methods Tutorial
let td = new Date();
console.log(td);
// Date Get Methods
console.log(td.getFullYear());
console.log(td.getMonth());
console.log(td.getDate());
console.log(td.getHours());
console.log(td.getMinutes());
console.log(td.getSeconds());
console.log(td.getTime());
// Date Set Methods
td.setFullYear(2026);
td.setMonth(2);
td.setDate(9);
td.setHours(11);
td.setMinutes(33);
td.setSeconds(45);
console.log(td);

JavaScript Date Methods Code
OjamboShop.com Web IDE JavaScript Date Methods Code

JavaScript Date Methods Result
OjamboShop.com Web IDE Displaying JavaScript Date Methods Result


Usage

You can use any IDE or text editor and the web browser to compile and execute JavaScript code. For this tutorial, the OjamboShop.com Learning JavaScript Course Web IDE can used to input and compile JavaScript code for the date methods.

Open Source

JavaScript follows the ECMAScript standard and is licensed under the W3C Software License by web browser vendors and runtime environment vendors. This allows commercial use, modification, distribution, and allows making derivatives proprietary.

Learn Programming Courses:

Get the Learning JavaScript Course for your web browser on any device.

OjamboShop.com Learning JavaScript Course
OjamboShop.com Learning JavaScript Interactive Online Course

Learn Programming Books:

Learning Javascript Book is available as Learning JavaScript Paperback or Learning JavaScript Ebook.

OjamboShop.com Learning JavaScript Book Announcement
OjamboShop.com Learning JavaScript Ebook And Paperback Announcement

Conclusion:

JavaScript makes it easy to use built-in date methods. Use the date methods to manipulate the current date, time or a specified date and time.

If you enjoy this article, consider supporting me by purchasing one of my OjamboShop.com Online Programming Courses or publications at Edward Ojambo Programming Books

References:

Leave a Reply

Your email address will not be published. Required fields are marked *