QGIS – How to Convert Date/Time Format

convertdateformatqgistime

My shapefile contains a attribute field called "created" (type QString) and this field holds the date and time of every feature in this format: "2022-03-04-13-28-26" (yyyy-mm-dd-hh-mm-ss)

I want to create a new attribute field that automatically converts this format into the following format: "dd.mm.yyyy hh:mm" (consider space between date and time and leave away the seconds)

How to do that?

I am using QGIS 3.16.6

Best Answer

A Field Calculator expression tested in QGIS 3.16.16

array_to_string(array_reverse(string_to_array(left("created", 10), '-')),'.')+' '+substr("created", 12, 2)+':'+substr("created", 15, 2)

enter image description here

which works like below:

array_to_string(                            // This will change the array to string
     array_reverse(                         // Re-orders [2022,03,04] to [04,03,2022]
         string_to_array(                   // Changes "created" to an array
             left("created", 10), '-')),'.' // Takes the first 10 chars
              )+' '+                        // Inserts a space
substr("created", 12, 2)+':'+               // Extracts hh and adds ':'
substr("created", 15, 2)                    // Extracts mm
Related Question