How to compare Python DateTime with Javascript DateTime?



Both Python and JavaScript have unique ways of representing date and time data. To compare Python datetime objects with JavaScript Date objects, we must ensure that both are converted to a common format, such as ISO 8601 strings or Unix timestamps (milliseconds since epoch).

The following are two major differences between Python (datetime) and JavaScript (date) objects.

  • Month Representation: JavaScript uses a 0-indexed month (0 for January, 11 for December), while Python uses a 1-indexed month (1 for January, 12 for December).
  • Default Time Zone: Python defaults to UTC, while JavaScript defaults to the user's local time zone.

Basic Date Creation and Comparison

Let's start with a simple tuple representing a date (2017, 11, 1). If we convert this to a datetime object in both Python and JavaScript, we will see different results because they handle the month argument differently.

Date Creation in Python

The following Python program creates a datetime object representing November 1, 2017, and prints it in UTC format.

from datetime import datetime

date_py = datetime(2017, 11, 1)
print(date_py)

Following is the output of the above code:

2017-11-01 00:00:00

Date creation in JavaScript

The following JavaScript code creates a Date object representing December 1, 2017, then prints the date in a user's local time zone.

let date_js = new Date(2017, 11, 1);
console.log(date_js.toString());

Following is the output of the above code:

Fri Dec 01 2017 00:00:00 GMT+0530 (India Standard Time)

Comparison of Both Results

The date in Python is 1st November 2017, but in JavaScript it becomes 1st December 2017. This is because JavaScript interprets 11 as December (indexing from 0).

Python's datetime object is timezone-naive by default, often assumed to be in UTC. JavaScript's Date object defaults to the local timezone of the system (e.g., GMT+5:30 for India).

Adjusting JavaScript Month Index (0-based to 1-based)

If we directly pass the Python-style date to JavaScript without adjusting the month argument, then we may get the wrong date. This is because JavaScript's month index starts from 0 (January) to 11 (December), while Python's datetime module uses a range from 1 to 12.

To correct this difference, we should subtract 1 from the Python month before creating a JavaScript Date. Respectively, we have to add 1 to the JavaScript month when parsing it back to Python.

Example

The following program converts a Python-style date (2025, 5, 26) to a JavaScript Date object. It adjusts JavaScript's 0-based month indexing, then prints the resulting date in a string format.

// Python style date: (2025, 5, 26)
var year = 2025;
var month = 5 - 1;  // Adjusting for JavaScript's 0-based month
var day = 26;

var jsDate = new Date(year, month, day);
console.log("Adjusted JavaScript Date:", jsDate.toDateString());

Following is the output of the above code:

Adjusted JavaScript Date: Mon May 26 2025

Using UTC Timezone in JavaScript

As we already discussed that JavaScript uses the local system's timezone by default, which can lead to differences when comparing with Python's UTC-based datetime. To fix this, JavaScript provides the Date.UTC() method, which returns the number of milliseconds since the epoch (January 1, 1970, UTC).

Example

In the following program, we converted JavaScript date representation (local system's time zone) into UTC Timezone by using Date.UTC() method.

var utc = Date.UTC(2013, 7, 10);
console.log("UTC timestamp:", utc);

Following is the output of the above code:

UTC timestamp: 1376092800000
Updated on: 2025-08-28T11:44:15+05:30

812 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements