3

How can I get rid of the time zone in the date/time field?

So I have a GeoPackage with the following data:

enter image description here

and want to get just 2021-06-07 16:25:40

4
  • Which field type is this?
    – Erik
    Commented Dec 6, 2021 at 13:30
  • @Erik it is date/time field Commented Dec 6, 2021 at 13:31
  • Are you sure? Because using 3.16 I can't find an option to add the timezone to the datetime.
    – Erik
    Commented Dec 6, 2021 at 13:35
  • @Erik yes, I'm sure. Got this value from string using parsing function (gis.stackexchange.com/questions/212274/…): "QDateTime.fromString(fromval, 'dd.MM.yyyy h:mm:ss')". The timezone was unwanted. Commented Dec 6, 2021 at 13:40

3 Answers 3

3

In my QGIS (3.22), when I use datetime format, I get timezone information included by default. You can convert it to a string to get rid of it - use this expression, where datetime is the name of the field containing the datetime-information:

left(to_string(datetime), 19)

However, you can't use the resulting string field as a datetime field any more. Including timezone makes sense - as 2021-12-06 14:48:41 is not the same everywhere on Earth.

2
  • thank you! so there is an issue with the version? Commented Dec 6, 2021 at 13:49
  • No, I think including timezone is intended, that's not an issue. This format is not primarily intended for visualization - if you want to visualize, best convert to a string.
    – Babel
    Commented Dec 6, 2021 at 14:06
3

After inspecting provided GeoPackage I could not find the problem in any of your datetime fields, specifically "Save_times1" and "Loc1".

fields

attribute_table


Proceed with RMC > Properties > Attributes Forms > Fields (see documentation) where change the Field Format from ISO Date Time into Date Time.

window

Before:
attribute_table1

After:
attribute_table2

7
  • it's already date time, I've checked. I will share my data in a second. Commented Dec 6, 2021 at 14:20
  • @Taras: I downloaded the Geopacke provided by kartographinya and indeed, I get the same format, including timezone: i.sstatic.net/D4lOM.png This, however, is a visualization issue and I guess it has to do with system settings - QGIS just takes the options used by the operation system. As you see in my screenshot, if you try to edit a field, the timezone disappers.
    – Babel
    Commented Dec 6, 2021 at 14:56
  • So the question is why the timezone bothers you? In case you use several timezones, it matters, of course. If that is the case, I think you should reformulate the question or ask a new one. If you just want to use the time as a text somewhere, without the disturbing timeone text, converting the field to a string as proposed in my answer should work.
    – Babel
    Commented Dec 6, 2021 at 14:57
  • @Taras which version of QGIS do you use? Commented Dec 7, 2021 at 7:20
  • QGIS 3.18. and you?
    – Taras
    Commented Dec 7, 2021 at 7:20
2

You need to go to the field calculator. Then tick update existing field. Select your desired field and type:

regexp_replace( "date" , '(MSK)', '')

Then hit OK. Good Luck.

4
  • 1
    thank you, but doesn't work. Commented Dec 6, 2021 at 14:02
  • You should replace "date" WITH YOUR FIELD NAME e.g. "date_field"! Then try again.
    – Ash
    Commented Dec 6, 2021 at 14:20
  • 1
    Yes, I've done that Commented Dec 6, 2021 at 14:22
  • try `regexp_replace( "date" , '\\s(MSK)', ''). It must have worked. I've got confused right now.
    – Ash
    Commented Dec 6, 2021 at 14:52

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.