xxxxxxxxxx
# importing datetime module
import datetime
import time
# assigned regular string date
date_time = datetime.datetime(2021, 7, 26, 21, 20)
# print regular python date&time
print("date_time =>",date_time)
# displaying unix timestamp after conversion
print("unix_timestamp => ",
(time.mktime(date_time.timetuple())))
xxxxxxxxxx
>>> from datetime import datetime
>>> ts = int("1284101485")
# if you encounter a "year is out of range" error the timestamp
# may be in milliseconds, try `ts /= 1000` in that case
>>> print(datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S'))
#'%Y' will be replaced by the year '%m' by the month '%d; by the day and so on
#You move these how you want in the string, other characters will be ignored!
'2010-09-10 06:51:25'
xxxxxxxxxx
import datetime
timestamp = 1610342400 # Replace with the Unix timestamp you want to convert
# Convert Unix timestamp to datetime
datetime_obj = datetime.datetime.fromtimestamp(timestamp)
# Format the datetime as desired
formatted_datetime = datetime_obj.strftime('%Y-%m-%d %H:%M:%S')
print(formatted_datetime)
xxxxxxxxxx
#get date from unix timestamp
from datetime import date
timestamp = date.fromtimestamp(1326244364)
xxxxxxxxxx
from datetime import datetime
# current date and time
currentDateTime = datetime.now()
print("Current Date Time is ", currentDateTime)
# convert datetime to timestamp
timestamp = datetime.timestamp(currentDateTime)
print("Current Unix Timestamp is ", timestamp)
xxxxxxxxxx
import datetime
print(
datetime.datetime.fromtimestamp(
int("1284105682")
).strftime('%H:%M:%S')
)
xxxxxxxxxx
>>> from datetime import datetime
>>> ts = int("1284101485")
# if you encounter a "year is out of range" error the timestamp
# may be in milliseconds, try `ts /= 1000` in that case
>>> print(datetime.utcfromtimestamp(ts).strftime('%H:%M:%S'))