1

I try to convert MySQl datetime to same in Python.On debug there is

ValueError: time data '2001-06-04T11:30:35' doesn't match format %Y-%m-%dT%H:%M:%S . In MySQL there is no 'T' in data.I tried format with 'T' and without.

I saw this article How to convert the following string to python date? .

This is code:

query = QSqlQuery ()
query.exec_("SELECT birthday FROM vista.user ")    
def countAge(birthday):
    birthday = datetime.strptime(str(birthday), "%Y-%m-%dT%H:%M:%S.%f")
    today = date.today()
    age = today.year - birthday.year
    if today.month < birthday.month:
        age -= 1
    elif today.month == birthday.month and today.day < birthday.day:
        age -= 1
    if age >= 0 :   
        return age
ages = []
index = 0
while (query.next()):
    print(query.value(index).toString())
    ages.append(countAge(query.value(index).toString()))
    index = index + 1

What is a problem?

3
  • So what is the output of: print(query.value(index).toString())? Commented Nov 22, 2016 at 20:59
  • 2001-06-04T11:30:35 Commented Nov 22, 2016 at 21:02
  • @ekhumoro , It was for test Commented Nov 22, 2016 at 21:11

1 Answer 1

2

If an example date-string is 2001-06-04T11:30:35, then you need:

    birthday = datetime.strptime(str(birthday), "%Y-%m-%dT%H:%M:%S")
Sign up to request clarification or add additional context in comments.

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.