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:

from datetime import datetime

date_py = datetime(2017, 11, 1)
print(date_py)
2017-11-01 00:00:00

Date Creation in JavaScript

The following JavaScript code creates a Date object with the same parameters, but notice the different result:

let date_js = new Date(2017, 11, 1);
console.log(date_js.toString());
Fri Dec 01 2017 00:00:00 GMT+0530 (India Standard Time)

Comparison of Both Results

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

Additionally, Python's datetime object is timezone-naive by default, while JavaScript's Date object defaults to the local timezone of the system.

Adjusting JavaScript Month Index

To create equivalent dates between Python and JavaScript, we must adjust for the 0-based month indexing in JavaScript:

// Python style date: (2025, 5, 26) = May 26, 2025
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());
Adjusted JavaScript Date: Mon May 26 2025

Using UTC in JavaScript

To match Python's UTC behavior, use JavaScript's Date.UTC() method, which returns milliseconds since the Unix epoch in UTC:

// Create UTC timestamp
var utcTimestamp = Date.UTC(2023, 7, 10); // August 10, 2023 UTC
console.log("UTC timestamp:", utcTimestamp);

// Create Date object from UTC timestamp
var utcDate = new Date(utcTimestamp);
console.log("UTC Date:", utcDate.toUTCString());
UTC timestamp: 1691625600000
UTC Date: Thu, 10 Aug 2023 00:00:00 GMT

Comparison Methods

Here are common approaches to compare Python and JavaScript dates:

Method Format Example
ISO String YYYY-MM-DDTHH:mm:ss.sssZ 2023-08-10T00:00:00.000Z
Unix Timestamp Milliseconds since epoch 1691625600000

Converting to ISO Format for Comparison

// JavaScript: Convert to ISO string
var jsDate = new Date(2023, 7, 10); // August 10, 2023
console.log("JS ISO:", jsDate.toISOString());

// For comparison with Python datetime.isoformat()
var utcDate = new Date(Date.UTC(2023, 7, 10));
console.log("JS UTC ISO:", utcDate.toISOString());
JS ISO: 2023-08-09T18:30:00.000Z
JS UTC ISO: 2023-08-10T00:00:00.000Z

Conclusion

When comparing Python datetime and JavaScript Date objects, remember to adjust for JavaScript's 0-based month indexing and timezone differences. Use ISO strings or UTC timestamps for reliable cross-language date comparisons.

Updated on: 2026-03-15T21:49:58+05:30

928 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements