A simple guide for working with dates in JavaScript

By Prajwal Haniya

Techletter #47 | October 29, 2023

JavaScript comes with a built-in date object. By default, new Date() creates an object corresponding to the current date and time. This will be created according to the current computer’s system settings.

const presentDate = new Date();
console.log(presentDate);

// Output: Sat Oct 28 2023 09:49:22 GMT+0530 (India Standard Time)
Day of the week Month Day Year Hour Minute Second Timezone
Sat Oct 28 2023 09 49 22 GMT+0530(India Standard Time)

Date methods

  1. getFullYear() : yyyy

  2. getMonth() : 0-11

  3. getDate() : 1-31

  4. getDay() : 0-6

  5. getHours() : 0-23

  6. getMinutes() : 0-59

  7. getSeconds() : 0-59

  8. getMilliseconds() : 0-999

  9. getTime() : milliseconds since January 1, 1970

Epoch time

Epoch time in JavaScript is the number of milliseconds elapsed since the Unix epoch, which is January 1, 1970, at 00:00:00 UTC. It is a standard way of representing time in computers.

There are two main ways to get the epoch time in JavaScript:

  1. Use the Date.now() method. This method returns the current timestamp in milliseconds since the epoch.

  2. Create a new Date object and use the getTime() method. This method returns the number of milliseconds since the epoch for the specified date.

// Get the current epoch time in milliseconds.
const currentEpochTime = Date.now();

// Create a new Date object for January 1, 1970, at 00:00:00 UTC.
const epochDate = new Date(0);

// Get the epoch time in milliseconds for January 1, 1970, at 00:00:00 UTC.
const epochEpochTime = epochDate.getTime()

What is the difference between Date and new Date()?

The main difference between the two is that Date() is a static constructor, while new Date() is an instance constructor.

A static constructor is a constructor that can be called without creating an instance of the class. An instance constructor is a constructor that creates an instance of the class when it is called.

Now, let us take the above quote and understand it in depth.

When I say “A static constructor in JavaScript is a constructor that can be called without creating an instance of the class.” This means that the static constructor can be used to initialize static variables and properties of the class, or to perform other tasks that do not require an instance of the class to be created.

When I say “An instance constructor in JavaScript is a constructor that creates an instance of the class when it is called.” This means that the instance constructor is used to initialize the instance variables and properties of the new object.

Let us understand this with an example:

class MyClass {
  static myStaticProperty = 10;

  constructor() {
    this.myInstanceProperty = 20;
  }
}

// Call the static constructor to initialize the static property.
MyClass.myStaticProperty; // 10

// Create a new instance of the class.
const myInstance = new MyClass();

// Access the instance property.
myInstance.myInstanceProperty; // 20

Examples of when to use a static constructor

Examples of when to use an instance constructor

What is the use of Static constructor Date when we have instance constructor new Date?

The static constructor is used to initialize the Date object’s internal state, such as the current time and date. The instance constructor is used to create a new Date object with a specific date and time.

Dates will always create a problem while interacting with the third party application. You need to be aware of the different timezones. So keeping time in millisecods will resolve most of the problems. Because, timezone of your server maybe different than where you are located.