I am using the time manager for a set of data and I need to create a column in my attribute table of the date in the format yyyy-mm-dd. I already have a column in my attribute table that contains Julian days (e.g. values of 1, 2, 3, etc. that I want to correspond to 2010-01-01, 2010-01-02, 2010-01-3, etc. respectively). I've tried using the field calculator to do this using Converting from Julian Date to Calendar Date? but I was unable to get an output.
2 Answers
Another approach, assuming your Day 1 starts on 2010-01-01
.
to_date(to_date('2010-01-01') + to_interval(concat(to_string(("j_day" - 1)), ' days')))
j_day
is your Julian day field
-
-
Thanks @BERA I wish I could do programming ... it will be my New Year resolution next year...– KazuhitoOct 13, 2017 at 7:22
-
Thanks! This is exactly what I was looking for. I'm unfamiliar with programming so this is more straightforward. Oct 13, 2017 at 17:09
-
Thanks @racheldeep for this interesting subject. It gave me opportunity to learn about calendar and Julian day (a.k.a. ordinal date).– KazuhitoOct 13, 2017 at 22:44
Add a date field
Use Field Calculator with function editor (change the start date, dont know what you want) and press blue play button on top:
from qgis.core import *
from qgis.gui import *
import datetime
@qgsfunction(args='auto', group='Custom')
def jtogreg(x, feature, parent):
startdate=datetime.datetime(2000,1,1)
d=startdate+datetime.timedelta(x)
return str(d)
The function can now be found under 'Custom'. Call the function:
jtogreg( "JDays" )