Last active
August 29, 2015 14:04
-
-
Save geogradient/6210d5e85d7867b5f9e6 to your computer and use it in GitHub Desktop.
Function to get the week number based on a given date_string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
__author__ = "[José M. Beltrán](<[email protected]>)" | |
__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