Easy Tutorial
❮ Python Func Number Acos Python Binary Search ❯

Python Convert Timestamp to Specified Date Format

Python3 Examples

Given a timestamp, convert it to a specified date format.

Note the timezone settings.

Current Time

Example 1

import time

# Get current timestamp
now = int(time.time())
# Convert to other date formats, such as: "%Y-%m-%d %H:%M:%S"
timeArray = time.localtime(now)
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
print(otherStyleTime)

Executing the above code outputs:

2019-05-21 18:02:49

Example 2

import datetime

# Get current time
now = datetime.datetime.now()
# Convert to specified format
otherStyleTime = now.strftime("%Y-%m-%d %H:%M:%S")
print(otherStyleTime)

Executing the above code outputs:

2019-05-21 18:03:48

Specified Timestamp

Example 3

import time

timeStamp = 1557502800
timeArray = time.localtime(timeStamp)
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
print(otherStyleTime)

Executing the above code outputs:

2019-05-10 23:40:00

Example 4

import datetime

timeStamp = 1557502800
dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
otherStyleTime = dateArray.strftime("%Y-%m-%d %H:%M:%S")
print(otherStyleTime)

Executing the above code outputs:

2019-05-10 23:40:00

Python3 Examples

❮ Python Func Number Acos Python Binary Search ❯