3

I'd like to create a new field adding 10 days in a date (string format) in QGIS; using the Field Calculator. For example, in the field_1 there is 22/02/2021, in field_new it should become 04/03/2021.

Maybe, I should convert date in number, as you can see in Excel.

enter image description here

Any suggestion?

2
  • 1
    What is type of the date field? String or Date? Feb 22, 2021 at 22:17
  • "field_1" is a string, also "field_new" will be a string
    – Mark
    Feb 23, 2021 at 7:49

2 Answers 2

5

You can use something like:

format_date(to_date("field_1",'dd/MM/yyyy')+to_interval('10 Days'),'dd/MM/yyyy')

First you need to convert your string to a date (the to_date() function), so you can add a datetime interval. The format_date() is needed, because the result of + to_interval() will return a datetime format. So you convert the result back to a date only and use your previous date-format as string (dd/MM/yyyy instead of yyyy-MM-dd).

1
  • This solution works!!
    – Mark
    Feb 23, 2021 at 21:02
5

For string-typed target field:

to_string( to_date( "field_1" , 'dd/MM/yyyy' ) + to_interval( '10 days' ) )

enter image description here


For date-typed target field:

to_date( to_date( "field_1" , 'dd/MM/yyyy' ) + to_interval( '10 days' ) )

enter image description here

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.