js date object


By gobrain

Handling date operations is crucial in any application. Therefore, every programming language has built-in methods for handling dates. JavaScript is one of these languages and provides a date object with its useful methods.

Let's dive into this object and its functional methods:

Creating a JavaScript Date Object

The Date object can be created using the new keyword followed by the Date() constructor. This creates a new instance of the Date object with the current date and time.

const today = new Date();
// Fri Mar 17 2023 22:11:09 GMT+0300 (GMT+03:00)

Alternatively, you can create a Date object with a specific date and time by passing in the desired values for year, month, day, hour, minute, second and millisecond to the Date() constructor.

const birthday = new Date(2000, 11, 25);
Mon Dec 25 2000 00:00:00 GMT+0200 (GMT+03:00)

Working with Dates and Times

The Date object provides a wide range of methods and properties that allow us to work with dates and times. Some of the most commonly used methods and properties include:

// Get the current date and time
var today = new Date();

// Get the year
var year = today.getFullYear();

// Get the month (0-11)
var month = today.getMonth();

// Get the day of the month (1-31)
var day = today.getDate();

// Get the day of the week (0-6)
var dayOfWeek = today.getDay();

// Get the hours (0-23)
var hours = today.getHours();

// Get the minutes (0-59)
var minutes = today.getMinutes();

// Get the seconds (0-59)
var seconds = today.getSeconds();

// Get the milliseconds (0-999)
var milliseconds = today.getMilliseconds();

Formatting Dates and Times

The Date object also provides a range of methods for formatting dates and times for display. These methods allow developers to format dates and times in a wide range of formats, including ISO 8601, UTC and localized formats.

const today = new Date();
// Format the date as ISO 8601
const isoDateString = today.toISOString();
// 2023-03-17T19:17:01.978Z 3/17/2023

// Format the date as a string in the local timezone
const localDateString = today.toLocaleString();
// 10:17:01 PM 3/17/2023

These methods allow us to format dates and times in a wide range of custom formats, including date and time separators, timezones and custom formatting strings.

Handling Timezones

One of the most important considerations when working with dates and times in JavaScript is handling timezones. The Date object provides methods for handling timezones, including setting and getting the UTC date and time.

const today = new Date();

// Get the current UTC date and time
const utcDate = today.toUTCString();
// Fri, 17 Mar 2023 19:22:58 GMT 3/17/2023

// Format the date as a string in the specified timezone
const timezoneDateString = today.toLocaleString("en-US", {
  timeZone: "America/New_York",
// 3:22:58 PM


Javascript has a wide range of built-in method from date object for handling time and date properly. These methods can be used get current date, handling timezone and formmatting date and time.

Thank you for reading.