Converting Epoch Time into the Datetime

Converting Epoch time into the datetime

To convert your time value (float or int) to a formatted string, use:

import time

time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1347517370))

For example:

import time

my_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1347517370))

print(my_time)

Converting epoch time to a date in time zone in python

1565475300 this is 2019-08-10 22:15:00 in UTC

datetime.fromtimestamp(1565475300, tz=timezone.utc)
datetime.datetime(2019, 8, 10, 22, 15, tzinfo=datetime.timezone.utc)

so it is 23:15 in London time zone

Converting epoch time with milliseconds to datetime

Use datetime.datetime.fromtimestamp:

>>> import datetime
>>> s = 1236472051807 / 1000.0
>>> datetime.datetime.fromtimestamp(s).strftime('%Y-%m-%d %H:%M:%S.%f')
'2009-03-08 09:27:31.807000'

%f directive is only supported by datetime.datetime.strftime, not by time.strftime.

UPDATE Alternative using %, str.format:

>>> import time
>>> s, ms = divmod(1236472051807, 1000) # (1236472051, 807)
>>> '%s.%03d' % (time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(s)), ms)
'2009-03-08 00:27:31.807'
>>> '{}.{:03d}'.format(time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(s)), ms)
'2009-03-08 00:27:31.807'

Convert epoch timestamp to datetime.datetime object

It's worthwhile to remember that a timestamp has no associated timezone information.
Unix time is unambiguously UTC, but it's not unusual to store local time in Unix-style timestamps. You must check against a known reference to be sure.


If your timestamps are already in UTC (Unix time), you can use Marc B's suggestion of subtract two unix timestamps directly to give you the number of seconds of difference between them.
You may then want to create a timedelta, which will allow you to easily manipulate datetimes with it (you can sum or subtract a timedelta to/from a datetime, for example).

datetime.timedelta( seconds= n_seconds )


If your timestamps are in local time, don't subtract them directly, as you'll get potentially incorrect results.

Instead, you should use datetime.fromtimestamp first, attach a timezone to each datetime, and only then subtract them to get the timedelta.

To convert back from a timedelta to the number of seconds, there's timedelta.total_seconds, assuming you're using python 2.7 . If you have an earlier version, you'll have to do the calculation by hand, or pip install datetime

Convert date in words to epoch time stamp in python

With the input from the comment you could try:

from datetime import datetime

dates = {'Date': ['May 01, 2022', 'Apr 30, 2022', 'Apr 29, 2022', 'Apr 28, 2022', 'Apr 27, 2022']}
dates['Date'] = [
datetime.strptime(date, "%b %d, %Y").timestamp() for date in dates['Date']
]

How to convert out of range epoch to datetime python?

The built in python datetime is limited by the number of seconds since the epoch 1970) you can store as an integer. As LMC correctly pointed out, the short answer to your question is “you can’t.”

The cftime library was developed for climate science, astronomy, and other applications where this temporal range isn’t sufficient. It has a datetime-compatible interface but supports a wider array of calendars and date ranges.

Check out cftime.num2date. I think it should work with your example out of the box as long as your integer is defined as seconds since 1970 in the standard Gregorian calendar.

Also, if you’re going to be working with CFTime alongside pandas or other pydata libraries, it’s worth checking out xr.cftime_range which will create a range index for use in resampling operations, etc.



Related Topics



Leave a reply



Submit