1

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?

2
  • %H is for a 24-hour clock and you are passing with an AM/PM. Change to ''%Y-%m-%d %I:%M:%S %p'. %I handles the 12-hour clock and %p handles the AM/PM. Commented May 22, 2018 at 16:07
  • Show some sample code. See the minimal reproducible example guidelines. In Excel, a time is a floating point value indicating the number of days since the epoch, so depending on how you fetch the Excel value you might be comparing a string to an float. Commented May 22, 2018 at 16:10

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.