What is the cause of the ValueError: year is out of range in Python? If you are looking for an answer to this problem, the following article is for you. I will show the cause of the error and some ways to solve it. Hope you will read the whole article.
What causes the ValueError: year is out of range in Python?
Dates and times are frequently stored in databases as timestamp values. A point on the time axis can be represented using a Unix timestamp or Unix time, which has its genesis at 00:00:00 on January 1, 1970 in Universal Time (UTC).
The ValueError: year is out of range in Python occurs because the timestamp object is out of the scope of the datetime.fromtimestamp()
function. You could set the timestamp object to microseconds.
Example:
from datetime import datetime timestamp = 1670748049273433 # Use the datetime.fromtimestamp function to convert a timestamp object to a datetime object datetimeObj = datetime.fromtimestamp(timestamp) print("Datetime object:", datetimeObj)
Output:
Traceback (most recent call last):
File "./prog.py", line 6, in <module>
ValueError: year 52945868 is out of range
The problem is that the timestamp object you set in microseconds should result in an error.
How to solve the ValueError: year is out of range in Python?
The timestamp object is in seconds
Example:
- Convert the timestamp object in the microsecond to seconds by dividing by 1000000.
from datetime import datetime timestampInMicroS = 1670748049273433 timestampInS = timestampInMicroS / 1000000 # Use the datetime.fromtimestamp function to convert a timestamp object to a datetime object datetimeObj = datetime.fromtimestamp(timestampInS) print("Datetime object:", datetimeObj)
Output:
Datetime object: 2022-12-11 08:40:49.273433
Use the datetime.now()
function
If you want to use it more succinctly, the datetime.now()
function from the datetime module is the right choice for you. The datetime.now()
function returns the current date and time.
Syntax:
datetime.now()
Example:
- Import datetime class from datetime module.
- Use the
datetime.now()
function to get the current date and time.
from datetime import datetime # Use the datetime.now() function to get the current date and time currentDt = datetime.now() print('Current date and time:', currentDt)
Output:
Current date and time: 2022-12-11 08:50:24.735029
Or you can use the datetime.today()
function.
Example:
from datetime import datetime # Use the datetime.today() function currentDt = datetime.today() print('Current date and time:', currentDt)
Output:
Current date and time: 2022-12-11 08:52:05.661798
Summary
Article on how to solve the ValueError: year is out of range in Python is over. The cause of the error is mainly because the fromtimestamp()
function is out of scope, so set the timestamp object to seconds.update
function correctly. If there are other ways or ideas about the article, please leave a comment.
Maybe you are interested:
- ValueError: must have exactly one of create/read/write/append mode
- ValueError: binary mode doesn’t take an encoding argument
- ValueError: dictionary update sequence element #0 has length N; 2 is required

My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.
Name of the university: HHAU
Major: IT
Programming Languages: C++, Python, Java