user2465134 user2465134 - 1 year ago 136
Python Question

Python - Convert a epoch timestamp to yyyy/mm/dd hh:mm

I'm given a timestamp (time since the epoch) and I need to convert it into this format:

yyyy/mm/dd hh:mm

I looked around and it seems like everyone else is doing this the other way around (date to timestamp).

If your answer involves
that would be great.

Answer Source

Using datetime instead of dateutil:

import datetime as dt
dt.datetime.utcfromtimestamp(seconds_since_epoch).strftime("%Y/%m/%d %H:%M")

An example:

import time
import datetime as dt

epoch_now = time.time()
>>> 1470841955.88

frmt_date = dt.datetime.utcfromtimestamp(epoch_now).strftime("%Y/%m/%d %H:%M")
>>> 2016/08/10 15:09

EDIT: strftime() used, as the comments suggested.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download