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.
Any suggestion?
-
1What is type of the date field? String or Date?Kadir Şahbaz– Kadir Şahbaz2021年02月22日 22:17:16 +00:00Commented Feb 22, 2021 at 22:17
-
"field_1" is a string, also "field_new" will be a stringMark– Mark2021年02月23日 07:49:18 +00:00Commented Feb 23, 2021 at 7:49
2 Answers 2
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).
For string-typed target field:
to_string( to_date( "field_1" , 'dd/MM/yyyy' ) + to_interval( '10 days' ) )
For date-typed target field:
to_date( to_date( "field_1" , 'dd/MM/yyyy' ) + to_interval( '10 days' ) )