3

I have a shapefile with a timestamp (string type) and id field (string type):

timestamp             tag  id
2017-11-14 09:03:00    1    0
2017-11-14 13:03:00    1    1
2017-11-14 20:03:00    1    2
2017-11-14 09:03:00    2    3
2017-11-14 13:03:00    2    4

I'm trying to find the time difference in hours between consecutive points by id using the Field Calculator tool in QGIS.

I tried the following expression but the results returned NULL. When I try to convert the timestamp field to DateTime using to_datetime function I get an error that it can not convert to DateTime. Any other suggestions on calculating the time difference with the Field Calculator in QGIS?

 second(age(attribute($currentfeature, to_datetime(timestamp)), 
           attribute(get_feature_by_id('points',$id-1), to_datetime(timestamp))))
1

1 Answer 1

5

This will do it:

hour(age(
 to_datetime(attribute($currentfeature,'timestamp')),
 to_datetime(attribute(get_feature_by_id('points',$id-1),'timestamp'))
))

Results are in hours as specified.
In my case, I used this to populate a column "timedif":

Time difference result


Difference in seconds would be:

second(age(
 to_datetime(attribute($currentfeature,'timestamp')),
 to_datetime(attribute(get_feature_by_id('points',$id-1),'timestamp'))
))

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.