ValueError: year is out of range in Python [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
2 min

banner

# ValueError: year is out of range in Python

The Python "ValueError: year is out of range" occurs when we pass a timestamp that is out of range to the fromtimestamp() method.

To solve the error, divide the timestamp by 1000 if it is in milliseconds.

valueerror year is out of range

Here is an example of how the error occurs.

main.py
from datetime import datetime timestamp_in_ms = 1654618085225 # โ›”๏ธ ValueError: year 54402 is out of range d = datetime.fromtimestamp(timestamp_in_ms)

timestamp should be in seconds

The datetime.fromtimestamp() method takes a timestamp in seconds, but we passed it a timestamp in milliseconds which caused the error.

# Convert the milliseconds to seconds

You can convert the milliseconds to seconds by dividing by 1000.

main.py
from datetime import datetime timestamp_in_ms = 1654618085225 d = datetime.fromtimestamp(timestamp_in_ms / 1000) print(d) # ๐Ÿ‘‰๏ธ "2022-06-07 19:08:05.225000"

convert milliseconds to seconds

A timestamp that is in milliseconds will have a length of 13.

main.py
timestamp_in_ms = 1654618085225 print(len(str(timestamp_in_ms))) # ๐Ÿ‘‰๏ธ 13 timestamp_in_seconds = timestamp_in_ms / 1000 print(timestamp_in_seconds) # ๐Ÿ‘‰๏ธ 1654618085.225

If you want to preserve the millisecond precision, use float division by dividing by 1000.0 instead.

main.py
from datetime import datetime timestamp_in_ms = 1654618085225 d = datetime.fromtimestamp(timestamp_in_ms / 1000.0) print(d) # ๐Ÿ‘‰๏ธ "2022-06-07 19:08:05.225000"

The datetime.fromtimestamp() method takes a timestamp in milliseconds and returns the corresponding local date and time.

If the optional tz argument is not provided, the timestamp is converted to the platform's local date and time.

The method raises ValueError if the timestamp is out of supported values.

# Using a try/except statement to handle the error

You can also use a try/except block to handle the error in a way that suits your use case.

main.py
from datetime import datetime timestamp_in_ms = 1654618085225 try: d = datetime.fromtimestamp(timestamp_in_ms) except ValueError: # ๐Ÿ‘‡๏ธ this runs print('except block runs') d = datetime.today() print(d) # ๐Ÿ‘‰๏ธ "2023-07-21 16:29:11.504679"

using try except statement to handle the error

We pass the timestamp to the fromtimestamp() method and if a ValueError is raised, the except block is run where we set the d variable to the current local date and time.

# Checking the length of the timestamp

An alternative way to solve the error is to check for the length of the timestamp before calling the fromtimestamp() method.

main.py
from datetime import datetime timestamp = 1654618085225 if len(str(timestamp)) == 13: d = datetime.fromtimestamp(timestamp / 1000) # ๐Ÿ‘‡๏ธ this runs print('The TS was in ms', d) # ๐Ÿ‘‰๏ธ "2022-06-07 19:08:05.225000" else: d = datetime.fromtimestamp(timestamp) print('The TS was in seconds', d)

checking the length of the timestamp

If the timestamp has a length of 13, it is most likely in milliseconds, so we divide it by 1000 to convert it to seconds before calling fromtimestamp().

Otherwise, we call the fromtimestamp() method directly with the timestamp.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev