Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert number in Excel time format to string equivalent in python

I extracted data from an Excel cell of time format m/d/yy h:mm with the number content ‘1645704206000’. When converted to its string equivalent which shows the time, it is 2/24/22 12:03.

How do I convert this number ‘1645704206000’ to 2/24/22 12:03 using python?

The timezone is in GMT timezone.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I am using Python 3.9 and Excel 2019.

>Solution :

It looks like there is a problem in the question related to Excel’s natural internal representation. But if you are interested in converting timestamps (with milliseconds) into a string, human-readable date, you can use datetime module in python:

from datetime import datetime
import pytz
tzHong = pytz.timezone('Hongkong')
tzGMT = pytz.timezone('Etc/GMT')
dateString = "1645704206000"
date = datetime.utcfromtimestamp(float(dateString)/1000)
print("Date in Hongkong: " + date.astimezone(tz=tzHong).strftime("%m-%d-%y %I:%M"))
print("Date in GMT: " + date.astimezone(tz=tzGMT).strftime("%m-%d-%y %I:%M"))

Output

Date in Hongkong: 02-24-22 08:03
Date in GMT: 02-24-22 12:03

Also note that the date you are showing (2/24/22 12:03) is in the GMT timezone, not Hongkong. Both time zones are shown in the code above. Make sure which one works for your desired output. Also, the answer provided by this answer is in 12-hour clock format. If you are interested in 24-hour format, just change %I in the code above with %H.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading