Date in Java Script

Manish SawManish Saw
4 min read

Introduction

Dates this is a thing that we watches more than 100 times in a Single Day, and it is said to be the Most Important thing of our life, which can be never be reversed. And In this blog, we are gonna to deep dive into Dates but in Java script. We will Understand how dates are calculated in JS backend, how we can modify and set it and mainly get it. What are the methods we can use in dates and everything else.


What is Date in Java script

In Java Script, Dates are Object which consist all needed things like Second, Minute, Hour, Year and even Milliseconds as Properties of the date object.

And you must had heard that “Java Script Date is Static”, it means that, Java script Dates cannot be updated automatically if you use that in your code.

Creating a Date

To create or construct Date in java script, we use “new Date()” constructor in this syntax”

let presentTime = new Date();

And If we print this Date, then this will look like this:

let presentTime = new Date();
console.log(presentTime);
// output: Thu May 15 2025 12:58:01 GMT+0530 (India Standard Time)

But it might looks differently, if you are different browser or runtime environments like node.js .


Internal Of Date

In Java Script, Date is an Object and it is calculated in milliseconds from January 01 1970.

In date Object, “month” and “week day” Properties are Zero indexed or their Index starts from 0. like value of “0th” month is January and “2nd” week day value is “Wednesday”.

When We uses Date, then it might feel easy, but actually it’s very complex because we also need to calculate time for each country or region because Every country can never have same time.

Most of us know that we can create or get Date, but only few knows that we can set date also.


Set Date

In Java script , we can also set date in this syntax:

let setDate = new Date(year,month,day,hours,minutes,seconds,milliseconds);

Example of Setting a Date:

let setDate = new Date(2025,4,15,13,25,40,60);

Now We don’t need to give all arguments for setting an Object. We can also give some or required arguments but it must in the same sequence(fist argument should be the Year and then months and …) :

let setDate = new Date(2025,4);

And If we only give one or two digit year, then js will take that like this “19XX”. EX:

// 0 - 99 are interpreted as 1900 - 1999
let dateWithTwoDigitYear = new Date(99, 0, 1); // Jan 1, 1999

// For one digit year
let dateWithOneDigitYear = new Date(9, 0, 1); // Jan 1, 1909

Methods for Date

Accessing Time Properties of Date Object

getDate() : Used to get Date of the Date Object :

// Create a new Date object for a specific date
const specificDate = new Date('2023-10-09');

// Use the getDate() method to retrieve the day of the month
const dayOfMonth = specificDate.getDate();

// Output the day of the month
console.log(dayOfMonth); // Output: 9

getMonth() :Used to get Month of the Date Object :

// Create a new Date object to get the current date and time
let currentDate = new Date();

// Use the getMonth() method to get the month (0-11) of the current Date object
let currentMonth = currentDate.getMonth();

// Output the current month number
console.log(currentMonth); // Outputs the month number (0 for January up to 11 for December)

Similarly there are “getFullYear” for getting year, “getHour” for getting Hours, “getMinute” for getting Minutes, and also for Seconds and Milliseconds.

Other useful Methods

There are lots of methods available for date object and some of the most Important among them are:

toDateString()": It returns those complex outputs into Human readable easy to read string like:

let date = new Date();
console.log(date)
// output: 2025-05-15T08:36:46.729Z

let readableDate = date.toDateString();
console.log(readableDate);
// output: Thu May 15 2025

We also have methods available to show date as per any specific Time zone standard like UTC(Coordinated Universal Time or Universal time) or ISO(International Organization for Standardization)

// Get the current date
const now = new Date();

// Display time in UTC standard
const utcString = now.toUTCString();

// Display time in ISO standard
const isoString = now.toISOString();

console.log(utcString);
// output: Thu, 15 May 2025 08:42:59 GMT

console.log(isoString);
// output: 2025-05-15T08:42:59.588Z

And we can also Know time as per any country Standard like this:

// Example usage
const now = new Date();

const indictTime = now.toLocaleString("hi-IN");    // India Time
const usTime = now.toLocaleString("en-US");    // US Time
const ukTime = now.toLocaleString("en-GB");    // UK Time
const jpTime = now.toLocaleString("ja-JP");    // Japan Time

console.log(usTime);
// output: 5/15/2025, 2:20:30 PM

console.log(ukTime);
// output: 15/05/2025, 14:20:30

console.log(jpTime);
// output: 2025/5/15 14:20:30

console.log(indictTime);
// output: 15/5/2025, 2:20:30 pm

Hope you found reading this blog Helpful. Make sure to click on the like Icon, if you found reading this blog Helpful.

0
Subscribe to my newsletter

Read articles from Manish Saw directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Manish Saw
Manish Saw