logo

Python Date And Time

Same project, different problem. This time the problem is dates and times. Specifically converting between them. These two small functions do just that.

# parse_date :: Date -> String
# e.g. parse_date("2001-12-31")
def parse_date(date):
return datetime.date(*strptime(date, "%Y-%m-%d")[0:3])

# str_date :: String -> Date
def str_date(date):
return date.strftime("%Y-%m-%d")

P.S. Pleac is a wonderful site, it aims to have a load of common code for a variety of languages, good to get practical tips as well as learn a new language.
«