I'm using QGIS 3.4 and I want to know how to convert a column datatype from string to datetime in python. I found that we can do that by to_datetime from the calculator but I want to do it from Python. Here is my data like: sample data


You can use the python datatime module:

import datetime
str_date = '2019-11-17 15:42:04'
date = datetime.datetime.strptime(str_date, '%Y-%m-%d %H:%M:%S')

In QGIS 3 you can use the following expression:

to_datetime('2019-11-17 15:42:04')
| improve this answer | |

The pandas package has a nice function that could help: pd.to_datetime

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.