From stat().st_mtime to datetime? From stat().st_mtime to datetime? python-3.x python-3.x

From stat().st_mtime to datetime?


You can use datetime.datetime.fromtimestamp, i.e.

from datetime import datetime, timezone...stat_result = path.stat()modified = datetime.fromtimestamp(stat_result.st_mtime, tz=timezone.utc)print('modified', modified)


This works for me if you want a readable string:

import datetimemtime = path.stat().st_mtimetimestamp_str = datetime.datetime.fromtimestamp(mtime).strftime('%Y-%m-%d-%H:%M')