

Here, we have specified the timestamp as -86400. To convert a negative UNIX timestamp to datetime, you can directly pass it to the fromtimestamp() method as shown above. from datetime import datetimeĭatetime_obj=omtimestamp(epoch) You can observe this in the following example. For example, the if we represent 31 December 1969 using epoch, it will evaluate to -86400 i.e 24 hours*3600 seconds before. So, if we represent a date before 1970 using an epoch, the value is negative. The UNIX timestamp or epoch is basically the number of seconds that elapsed after UTC 1st January 1970, 0 hours, 0 minutes, 0 seconds. Convert Negative Timestamp to Datetime in Python

Then, we used the timestamp() method to convert datetime object to timestamp. In this example, we first obtained the current datetime using the datetime.today() method. The timestamp() method, when invoked on a datetime object, returns the UNIX epoch for the given datetime object. To convert a datetime object to UNIX timestamp in python, we can use the timestamp() method. You can also change the position of the placeholders in the string to change the date format. In this example, the format specifiers used in the strftime() method are as follows. from datetime import datetimeĭatetime_string=datetime_obj.strftime( "%d-%m-%Y %H:%M:%S" ) The strftime() method, when invoked on a datetime object, takes the format of the required datetime string as its input argument and returns the output string. Then, you can use the strftime() method to convert the datetime object to a string. To convert the Unix timestamp to a datetime string in python, we will first create a datetime object from the epoch using the fromtimestamp() method or the utcfromtimestamp() method. This difference is due to the reason that the time zone of my computer is set to +5:30 hours. However, we have used the same epoch value. In this example, you can observe that the datetime output show the time approx 5 hours and 30 minutes before the time in the previous example. from datetime import datetimeĭatetime_obj=datetime.utcfromtimestamp(epoch) If you want to get the UTC time from the timestamp, you can use the utcfromtimestamp() instead of the fromtimestamp() method as shown below. The above approach will give you time according to the time zone on your machine. epoch.In this example, we have converted the epoch 123456789 to datetime value using the fromtimestamp() method. Here’s how you can use the script: :~/proj/python $. utc ) print ( "Time/date: ", format ( timedate )) print ( "Time/date in UTC: ", format ( timedate_utc )) except ValueError : print ( "Timestamp should be a positive integer, please." ) else : print ( "Usage: epoch.py " )įIXME: I’ll revisit this to re-publish script directly from GitHub. fromtimestamp ( timestamp ) timedate_utc = datetime. argv ) if timestamp > 0 : timedate = datetime. argv ) > 1 : print ( "This is the Epoch time: ", sys. Import sys from datetime import datetime, timezone if len ( sys. epoch.py script #!/Library/Frameworks/amework/Versions/3.6/bin/python3

But because I also like seeing time in UTC, I decided to use timezone module as well. Most of the functionality is done using the fromtimestamp function of the datetime module. Today I decided to write a simpe converter that takes Epoch Time as a parameter and returns you the time and date it corresponds to.
Epoch time converter code#
I’m slowly improving my Python skills, mostly by Googling and combining multiple answers to code a solution to my systems administration tasks.
