In NodeJs, why is new Date constructor setting default time to 7am?


NodeJS

Author: Steve Alila
Reviewer: Deepak Prasad

In Nodejs, the new Date constructor time defaults to 7 am if your time zone is 7 hours behind the Greenwich Meridian Time.

It does that to prevent inconsistencies among internet users when they use different time formats. Although you expect locale time, Nodejs displays your Operating System's time settings.

You should learn time standards and settings and Nodejs new Date Constructor to understand why the time differs amongst users in various regions.

 

Time zones and standards

A time zone is a region on earth that is bound by longitudes. Longitudes run vertically on the globe and are 15 degrees apart, breaking the world into 24 time zones. Each rotation of the earth creates a time difference of one hour per zone.

The history of time zones dates back to the 1880s when it was decided at the International Meridian Conference that time starts at Greenwich, London. The resulting system consisting of 24 time zones was referred to as Greenwich Mean Time (GMT). The GMT is often referred to as the Zulu or UTC. By the 1930s, most countries had aligned their times according to the GMT.

The Time Zone (TZ) Database compiles information about the world's time zones. The time zones (often referred to as IANA) create consistency while handling time in operating systems and computer programs.

A time standard is a specification for measuring time. For example, at what rate does time pass? At what point in time do you refer to? The reason for time standards is to prevent challenges, like daylight saving time, resulting when synchronizing time globally.

 

Difference between the Locale and OS time settings

The locale time is the time for your current time zone. On the other hand, an operating system (OS) counts the number of seconds since the Unix time (January first, 1970, 00:00:00).

Unlike the locale time, the Unix time (that Nodejs uses) is a system that describes a point in time. Here is what you should know about the Nodejs new Date constructor.

 

What is Nodejs new Date constructor?

It would help understand the concept of a constructor in JavaScript before focusing on Nodejs new Date constructor.

In object-oriented programming, a constructor is a unique method that runs whenever an instance of a class is created, assigning object arguments to the class attributes.

Most object-oriented programming languages demand creating a static class. That implies you must supply the constructor with the specified inputs when instantiating an object. Another point related to static classes is the abstraction principle of object-oriented programming, which demands interacting with the class attributes through the getter and setter methods.

Although JavaScript has class implementations, it still massively implements objects through its native dynamic constructor. The constructor lets you create an empty object and later fill it with inputs.

The constructor is a function whose name starts with a capital letter.

function Gamer (name, position, salary) {
  this.name = name
  this.position = position
  this.salary = salary
}

An instance of the constructor would be player1.

const player1 = new Gamer('John Doe', 07, 189000)

The new keyword grabs inputs from the object and fills the Gamer constructor with them through this keyword.

The built-in new Date constructor has similar arrangements.

const d = new Date()

Here, we create an object, d, from JavaScript's native Date constructor. Besides, we can get a string output by dropping the new keyword.

const d1 = Date()

The difference between `new Date()` and Date() is that the new Date() is an object counting the number of seconds that have passed since the Unix time. On the other hand, Date() is a date string that primary outputs locale time for uncomplicated display.

You can notice the difference by logging the two expressions (d and d1) in the browser and Nodejs.

 

Compare the UTC and locale time step-by-step

Let's see the result of logging the date constructor in the browser and Nodejs.

Create two files: index.html and main.js. Open the current folder with a code editor. I am using Visual Studio Code. Before that, you should have installed Nodejs on your system.

node -v

touch index.html main.js

code .

In NodeJs, why is new Date constructor setting default time to 7am?

 

Link main.js to index.html.

In NodeJs, why is new Date constructor setting default time to 7am?

 

Open the main.js file and add d and d1 as follows.

// with the new keyword
const d = new Date()

// without the new keyword
const d1 = Date()

// log results
console.log('With the new keyword => ', d)
console.log('Without the new keyword => ', d1)

In NodeJs, why is new Date constructor setting default time to 7am?

 

Let's see the result in the Nodejs and the browser consoles.

The result in Nodejs is:

With the new keyword => 2022-04-17T13:29:43.554Z

Without the new keyword => Sun Apr 17 2022 16:29:43 GMT+0300 (East Africa Time)

while that of the browser is:

main.js:8 With the new keyword => Sun Apr 17 2022 16:28:27 GMT+0300 (East Africa Time)

main.js:9 Without the new keyword => Sun Apr 17 2022 16:28:27 GMT+0300 (East Africa Time)

Nodejs new date constructor

It turns out that Nodejs expresses time in UTC when you use the new Date constructor.

2022-04-17T13:29:43.554Z

The first part (before T) represents the year, month, and date. T is a time separator. The second part, 13:29:43.554Z, implies the current time in Zulu (represented by Z) time is 1 pm, 29 minutes, 43 seconds, and 554 milliseconds. That means Nodejs new constructor uses the OS settings, which expresses time in UTC.

On the other hand, the browser converts the Zulu time and outputs the current locale time.

Sun Apr 17 2022 16:29:43 GMT+0300 (East Africa Time)

GMT+0300 means my local time (EAT) is 3 hours ahead of the GMT (or Zulu) time.

 

What if you specify the exact date inside the constructor?

Assume we want to show a specific time on a particular date. For example, let's see the time on February twentieth, 2022.

const d = new Date(2022, 1, 20)

This time around, Nodejs new constructor tells me the time was 9 pm: 2022-03-19T21:00:00.000Z. Let me try with a new date: April seventeenth, 2022, and May twenty-fifth, 2025.

// with the new keyword
const d = new Date(2022, 1, 20)
const d0 = new Date(2022, 3, 17)
const d1 = new Date(2025, 4, 25)

// log results
console.log(d)
console.log(d0)
console.log(d1)

The more exciting part is that even if I input a different date, Nodejs new constructor still tells me the time is 9 pm.

Likewise, the default time differs depending on your time zone. For example, if you are located in the US Pacific Time, in Nodejs, the new Date constructor setting default time is 7 am.

2022-02-19T07:00:00.000Z
2022-04-16T07:00:00.000Z
2025-05-24T07:00:00.000Z

That means, according to GMT, your midnight is at 7 am in London. Likewise, a developer in New Delhi would have a different default time.

Since the time is relative to GMT, it is easier to synchronize time among developers and internet users in various time zones. You can also simplify the process using libraries like moment.js.

 

Conclusion

In Nodejs, the new Date constructor time defaults to 7 am to ease time synchronization amongst internet users. The time difference between time zones, the browser, and Nodejs may confuse you while implementing time in JavaScript.

However, with adequate knowledge of the time standards and the Date constructor, you can comfortably use date in Nodejs.

 

Steve Alila

Steve Alila

He specializes in web design, WordPress development, and data analysis, with proficiency in Python, JavaScript, and data extraction tools. Additionally, he excels in web API development, AI integration, and data presentation using Matplotlib and Plotly. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment