How to Solve Python ValueError: year is out of range

by | Programming, Python, Tips

If you try to pass a timestamp to the datetime fromtimestamp() method that is out of range, you will raise the ValueError: year is out of range. This error typically is a result of passing a timestamp in milliseconds, while the fromtimestamp() method takes the timestamp in seconds.

You can solve this error by dividing the timestamp in milliseconds by 1000 to get the timestamp in seconds. For example,

from datetime import datetime

timestamp_ms = 1657577891147

timestamp_sec = timestamp_ms / 1000

current_dt = datetime.fromtimestamp(timestamp_sec)

This tutorial will go through the error in detail and how to solve it with code examples.


Python ValueError: year is out of range

In Python, a value is information stored within a particular object. We will encounter a ValueError in Python when we use an operation or function that receives an argument with the right type but an inappropriate value.

An integer representing timestamp is a suitable type for the datetime.fromtimestamp() method, but expects values between the years 1970 and 2038 (at the time of writing this article). If we pass a timestamp outside of this range, the value is inappropriate, and the Python interpreter will raise the ValueError.

Example

Let’s look at an example of trying to convert a timestamp to a datetime object. First, we will get the current time in milliseconds.

current_time_in_ms = 1657578261543

Next, we will attempt to pass this variable as the argument for the datetime.fromtimestamp() method, to convert it into a datetime.

from datetime import datetime

current_dt = datetime.fromtimestamp(current_time_in_ms)

print(current_dt)

Let’s run the code to see what happens:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [19], in <cell line: 3>()
      1 from datetime import datetime
----> 3 current_dt = datetime.fromtimestamp(current_time_in_ms)
      5 print(current_dt)

ValueError: year 54496 is out of range

The error occurs because we passed a timestamp value in milliseconds, whereas the fromtimestamp() method expects the timestamp in seconds.

Solution

We can solve this error by dividing the timestamp value by 1000 to convert it to seconds.

Let’s look at the revised code:

from datetime import datetime

current_time_in_ms = 1657578261543

current_time_in_s = current_time_in_ms / 1000

current_dt = datetime.fromtimestamp(current_time_in_s)

print(current_dt)

Let’s run the code to get the result:

2022-07-11 23:24:21.543000

We successfully retrieved the local datetime corresponding to the timestamp in seconds.

Summary

Congratulations on reading to the end of this tutorial!

For further reading on Python ValueErrors, go to the article:

How to Solve Python ValueError: empty separator

To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee