6

I have an attribute column that has a string datetime and I want to convert that to a date column. I first attempted to simply add the time column in an expression, and I only got the year/month/date, but I wanted the time as well. So I first converted it the string to a new column with format_date(), to clean up the format, and then I went ahead and used to_datetime() to convert the string into the date column. It shows up fine in the Expression dialogue but then doesn't add the hour/minute/seconds.

What am I overlooking?

Using QGIS version 3.16.1-Hannover on Windows

enter image description here

2
  • Which one is your source column? What file format is it and which datatype did you choose?
    – MrXsquared
    Commented Jan 3, 2021 at 23:31
  • It was because I converted a CSV to a shapefile, so problem solved. Shapefiles apparently don't store date and time in same field.
    – DrewGIS
    Commented Jan 4, 2021 at 0:35

2 Answers 2

8

You probably use shapefile format. Otherwise it should have worked.

According to the documentation shapefile format doesn't store date and time in the same field. You need to add date and time to seperate columns or use other format like GeoPackage.

2
  • You know that was it, I didn't know that about shapefiles. I converted from a CSV to a shapefile and it went all wonky. I'm using a geopackage now and it works perfectly.
    – DrewGIS
    Commented Jan 4, 2021 at 0:33
  • I have just learned that after your question. :) I have not used date and time in shapefile so far. Commented Jan 4, 2021 at 0:53
0

I had similar problem with column "IGNITIONDA" with values like "1930/11/01 00:00:00.000".

I converted this to only date column (but you can also add the time) in two steps. first you need to convert the format from string to either only date or only time or with combination with command:

format_date("IGNITIONDA" , 'yyyyMMdd')

Running this will create a column (here I named "ig_date") with only string having value "19301101". Now convert this string to the date with command to new column with date type:

to_date("ig_date",'yyyyMMdd')

Running the above commands will give the column with value type

<date: 1930-11-01>

Your Answer

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

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