Skip to content

Instantly share code, notes, and snippets.

@geogradient
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geogradient/6210d5e85d7867b5f9e6 to your computer and use it in GitHub Desktop.
Save geogradient/6210d5e85d7867b5f9e6 to your computer and use it in GitHub Desktop.
Function to get the week number based on a given date_string
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = "[José M. Beltrán](<beltran.data@gmail.com>)"
__credits__ = ["José M. Beltrán"]
__license__ = "GPL-3.0"
def date_to_week_number(date_string):
"""
:param date_string: input a date_string with format YYYYmmdd
:return: the week number for the given date_string
"""
import datetime
fmt = '%Y%m%d' # YYYYmmdd format
dt = datetime.datetime.strptime(str(date_string), fmt)
# isocalendar Returns a 3-tuple, (ISO year, ISO week number, ISO weekday)
# See https://docs.python.org/2/library/datetime.html
wk = dt.isocalendar()[1]
return wk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment