Skip to main content
Solved

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

  • January 12, 2021
  • 2 replies
  • 189 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.

 

 

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

2 replies

jdh
Contributor
Forum|alt.badge.img+40
  • 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

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.