Python 3 (Win) Is there a string var with date format YY-MM-DD how to get date?

  • Do you have difficulty converting a string to a date object? Or how having date object to receive week number? If the first, then ask a separate question. If the second, then see Definition of the week by date - jfs
  • Difficulties with the first one I know how to get a number - CMDmitry
  • try strptime() - jfs

3 answers 3

It's simple, you can use strftime:

 datetime.datetime.today().strftime('%Y-%m-%d') 

Or you can through datetime

 from datetime import * def convert_strtodate(date: string) -> datetime: return datetime.strptime(date, '%y-%m-%d') 
     from datetime import datetime def convert_str2date(date: str) -> datetime: return datetime.strptime(date, '%y-%m-%d') 

    The function accepts a YY-MM-DD format string and converts it to a datetime object.

       import datetime today = datetime.datetime.today() print(today.strftime("%Y-%m-%d-%H.%M.%S")[8:10]) # -> 08 
      • This is the answer to what question? - Enikeyschik
      • Enikeyschik And what I did not understand the question? explain then ... - Alexander