Skip to main content
Solved

Extract the date of the Saturday, based on week number.

  • January 12, 2021
  • 2 replies
  • 166 views

dustin
Influencer
Forum|alt.badge.img+31

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).

Best answer by jdh

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.

 

 

View original
Did this help you find an answer to your question?

2 replies

jdh
Contributor
Forum|alt.badge.img+28
  • Contributor
  • Best Answer
  • January 12, 2021

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.

 

 


dustin
Influencer
Forum|alt.badge.img+31
  • Author
  • Influencer
  • January 13, 2021
jdh wrote:

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.


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings