Skip to main content

I am need to extract the date of the Saturday of an given week, based on a yyyy/wk formatted number.

 

For example, I have an attribute with "202102". I need to convert this to be "20210109"...January 9th, 2021. (January 1st and 2nd counts as the first week).

Unfortunately the DateTimeConverter does not accept week flags an input, so we can't do something as simple as %G%V%w.

 

We can do this in python with the datetime module. The caveat is that ISO weeks start with an index of 0, not 1. So 202102 would be the 3rd week in january and therefore January 16th, not 9th. 

 

import datetime
 
def processFeature(feature):
    yw = str(feature.getAttribute('input'))
    d = datetime.datetime.strptime(yw+'6',  '%G%V%w')
    o = d.strftime('%Y%m%d')
    feature.setAttribute("Date", o)

The 6 in the code above indicates that you want saturday.

 

 


Unfortunately the DateTimeConverter does not accept week flags an input, so we can't do something as simple as %G%V%w.

 

We can do this in python with the datetime module. The caveat is that ISO weeks start with an index of 0, not 1. So 202102 would be the 3rd week in january and therefore January 16th, not 9th. 

 

import datetime
 
def processFeature(feature):
    yw = str(feature.getAttribute('input'))
    d = datetime.datetime.strptime(yw+'6',  '%G%V%w')
    o = d.strftime('%Y%m%d')
    feature.setAttribute("Date", o)

The 6 in the code above indicates that you want saturday.

 

 

Thank you very much. This works well for me.


Reply