Home » excel » Using datetime in Python and how to import from Excel

Using datetime in Python and how to import from Excel

Posted by: admin May 14, 2020 Leave a comment

Questions:

In python, I am using the command start = datetime.datetime.strptime(a, '%Y-%m-%d %H:%M:%S') and in Excel I have the date and time as 11/11/2005 12:00:00 AM. When I try to run the code, it is telling me that the time in excel does not match the code. How do I fix this?

How to&Answers:

Your string representation of your date is a little wrong.

Try:

import datetime
a = "11/11/2005 12:00:00 AM"
start = datetime.datetime.strptime(a, '%d/%m/%Y %H:%M:%S %p')
print(start)

Output:

2005-11-11 12:00:00

Answer:

This is what I have managed to get:

enter image description here

With this code:

import xlsxwriter
import datetime

workbook = xlsxwriter.Workbook('myxls.xlsx')
starting = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
worksheet = workbook.add_worksheet()
worksheet.write('A1',starting)