Skip to content

Instantly share code, notes, and snippets.

@serashioda
Last active January 29, 2017 09:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save serashioda/3619a51d4c66cabeec55ada8ea16e24a to your computer and use it in GitHub Desktop.
Save serashioda/3619a51d4c66cabeec55ada8ea16e24a to your computer and use it in GitHub Desktop.
import datetime
import datefinder
from dateutil.relativedelta import relativedelta
def date_rollback(large_string, date_formatter):
dates = list(datefinder.find_dates(large_string))
result = large_string
for date in dates:
to_find = date.strftime("%m/%d/%Y")
rollback = date - relativedelta(years=100)
datemod = rollback.strftime(date_formatter)
result = result.replace(str(to_find), str(datemod))
return result
result = date_rollback("I moved to the U.S. on 06/21/1993 but moved to Nevada on 07/12/2008.", "%d/%m/%Y")
print(result)
Write a function that takes two arguments, one large string and one smaller string.
The larger string will contain (amongst other text) dates of the format MM/DD/YYYY.
The smaller string will contain a date format, like "%b %d, %Y" or "%d/%m/%Y" or any valid date format string.
Your job will be to return the large text string with the dates rolled back by one century and formatted with whatever format was in the smaller string.
The dates you should consider will be any year between 1100 and 9999 AD.
Submit this thru canvas on the class website, you should be able to find the assignment...I will post a link
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment